views:

80

answers:

6

I know this is a proper enum:

Private Enum Months
  JANUARY = 1
  FEBRUARY = 2
  ...
End Enum

However, I want to have an enum where the string will solely be integers.

Example:

Private Enum ColumnCounts
  01 = 5
  02 = 4
  03 = 40
End Enum

The 01, 02 and 03 are all strings. However, if I put "01", [01] or just 01, it tells me it expects the end of the Enum and that it isn't a valid statement. Is there any way to accomplish this that I'm missing? I even tried. 01.ToString(), but that wasn't an option. :) Any and all help is appreciated. Thanks.

Edit:

Public Class Part

  Private Enum ColumnCounts
    f01 = 39
  End Enum

  Public Shared Function ValidateColumns(ByRef lstFields As List(Of String)) As Boolean
    For Each colCount In [Enum].GetValues(GetType(ColumnCounts))
      If colCount = "f" + lstFields(1) Then
        'Right here I need to compare the count of the list vs. the value of the enum.'
        Return True
      End If
    Next

    Return False
  End Function
End Class

Essentially, I didn't want to put the f in there, just wanted to do 01. The way this will be called is:

Select Case (nRecordType)
  Case "01"
    ...
  Case "02"
    ...
  Case "03"
    Return Part.ValidateColumns(_lstFields)
End Select

Because I'm not making an instance of it and not calling a constructor, there's no way to autopopulate a Dictionary. And I don't want to convert to integer so I'm not going to do an array. That is just in case eventually the value gets above 99 and the next value would be A0. I'm trying to think of easy future changes to this and backwards compatability. If you need more explanations, let me know.

Edit 2:

This is what I've done now and I think it should work:

Public Class Part

  Private Shared columnCounts As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)

  Public Shared Function ValidateColumns(ByRef lstFiels As List(Of String)) As Boolean
    InitializeColumnDictionary()

    Return lstFields.Count = columnCounts(lstFields(1))
  End Function

  Private Shared Sub InitializeColumnDictionary()
    columnCounts.Add("01", 39)
  End Sub
End Class

I'm not at a point where I can test it right now so I can't verify that it's going to do what I want to, but it doesn't give me an error when I build it so I'm crossing my fingers.

+3  A: 

Integers alone are NOT valid identifiers. Why would you want to call 01 an identifier? Prefix the enum elements with a letter, and you're done:

i01,
i02
...

edit as per your editing: declare a static dictionary and populate it, if it's null, in your static method:

private static void yourMethod()
        {
            if (ColumnCounts == null)
            {
                ColumnCounts = new Dictionary<String, int>();
                ColumnCounts.Add("01", 39);
                ColumnCounts.Add("02", 45);
                ColumnCounts.Add("03", 12);
                ColumnCounts.Add("04", 0);
            }




        }



        private static Dictionary<String, int> ColumnCounts = null;
vulkanino
I'm taking a string from a file, splitting it, and using a field to determine the column count. The string for that field could be 01, 02, etc. and I don't want to convert it to an integer because we don't know if in the future it will overflow the two places allotted for it.
XstreamINsanity
I'll be accepting this one as the answer since it's what I'll have to go with and it was the first answer of this sort.
XstreamINsanity
still I don't get it. you want to compare a string with an enum description? maybe you don't need an enum for that, but a dictionary to map a string to an integer.
vulkanino
@vulkanino - I'm going to update my post with an example. Check it out in 2 minutes.
XstreamINsanity
+2  A: 

Enum elements are identifiers; and identifiers must start with a letter (or underscore). So, I am sorry, but this is not possible.

Note: corrected start characters after a comment.

Timores
Not quite accurate - an identifier can start with an underscore, for example.
Dan Puzey
:( I so hope you're wrong, but this is what I was thinking as well. Might be best just to use a dictionary or something.
XstreamINsanity
@Dan - lol. Nice, but I think he meant any non-numeric character. Even then, I don't think slashes or asterisks would work.
XstreamINsanity
+1  A: 

You need to use valid identifiers - which cannot begin with a number.

Oded
A: 

Sorry, it's not possible to let an identifier of any kind start with an integer. You could do:

Private Enum ColumnCounts
  n01 = 5
  n02 = 4
  n03 = 40
End Enum
Joachim VR
A: 

For your enum you should use Column01 for the identifier. Later in your code, if you don't need the 01 portion, you can always call .ToString() and trim the Column portion out.

Private Enum ColumnCounts 
  Column01 = 5 
  Column02 = 4 
  Column03 = 40 
End Enum 
Joel Etherton
A: 

$.02

Private Enum foo As Integer
    _01 = 1
    _02 = 2
    _03 = 3
    _04 = 4
End Enum

Dim bar As IEnumerable(Of String) = (From membr In [Enum].GetNames(GetType(foo)) Select membr.Replace("_", ""))

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim nRecordType As String = "03"

    Select Case nRecordType
        Case bar(0)
            Debug.WriteLine("stop1")
            Stop
        Case bar(1)
            Debug.WriteLine("stop2")
            Stop
        Case bar(2)
            Debug.WriteLine("stop3")
            Stop
        Case bar(3)
            Debug.WriteLine("stop4")
            Stop
    End Select

End Sub
dbasnett