XstreamINsanity
XstreamINsanity

Reputation: 4296

How to have an integer string in an enum?

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.

Upvotes: 2

Views: 3338

Answers (6)

dbasnett
dbasnett

Reputation: 11773

$.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

Upvotes: 0

Timores
Timores

Reputation: 14589

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.

Upvotes: 2

vulkanino
vulkanino

Reputation: 9124

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;

Upvotes: 3

Joel Etherton
Joel Etherton

Reputation: 37523

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 

Upvotes: 0

Joachim VR
Joachim VR

Reputation: 2340

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

Upvotes: 0

Oded
Oded

Reputation: 498904

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

Upvotes: 1

Related Questions