\$\begingroup\$
\$\endgroup\$
1
Each letter represent a category. Example the group with "M" Code is greater Than "I" and "C" group. "F" is the higher group
Here is what I did but I wonder if there is a better way to do it.
Private Function CompareCode(ByVal code As String, ByVal otherCode As String) As Boolean
Dim orderedCode As New List(Of String)
orderedCode.Add("I")
orderedCode.Add("C")
orderedCode.Add("M")
orderedCode.Add("F")
Return orderedCode.IndexOf(code) < orderedCode.IndexOf(otherCode)
End Function
-
1\$\begingroup\$ Would you mind to add a little bit of context about what this method should do ? \$\endgroup\$Heslacher– Heslacher2014年12月03日 13:41:25 +00:00Commented Dec 3, 2014 at 13:41
1 Answer 1
\$\begingroup\$
\$\endgroup\$
6
Naming
The name CompareCode
does not express ll what this method is doing. Try to name it more meaningful.
General
You don't need a List(Of String)
here. Add a private const string like
Private Const orderedCode As String = "ICMF"
Private Function CompareCode(ByVal code As String, ByVal otherCode As String) As Boolean
Return orderedCode.IndexOf(code) < orderedCode.IndexOf(otherCode)
End Function
answered Dec 3, 2014 at 13:47
-
\$\begingroup\$ If for some reason he must use a
List(Of String)
, this might be cleaner:Dim orderedCode As New List(Of String) From { "I", "C", "M", "F" }
. \$\endgroup\$Abbas– Abbas2014年12月03日 13:49:33 +00:00Commented Dec 3, 2014 at 13:49 -
\$\begingroup\$ I agree with the problem of method name. thx \$\endgroup\$Jeanseb– Jeanseb2014年12月03日 13:50:22 +00:00Commented Dec 3, 2014 at 13:50
-
\$\begingroup\$ @Abbas I would agree, about some reason..., if the List had been declared outside of the method. \$\endgroup\$Heslacher– Heslacher2014年12月03日 13:51:57 +00:00Commented Dec 3, 2014 at 13:51
-
\$\begingroup\$ The list is not required and is a big part of the reason I'm asking for a better way. \$\endgroup\$Jeanseb– Jeanseb2014年12月03日 13:52:44 +00:00Commented Dec 3, 2014 at 13:52
-
\$\begingroup\$ Then you should indeed never use a List and use the solution @Heslacher provided. \$\endgroup\$Abbas– Abbas2014年12月03日 14:19:01 +00:00Commented Dec 3, 2014 at 14:19
lang-vb