-1

I have an enum that goes like this:

Enum Cards
   club = 1
   spade = 2
   hearts = 3
   diamond = 4
End Enum

Now, what I want to do is whenever I enter the string "club" or whatever from enum, I can get its integer value.

Thanks :)

Ralph De Guzman
  • 209
  • 3
  • 12
  • 1
    possible duplicate of [Parse a string to an Enum value in VB.NET](http://stackoverflow.com/questions/852141/parse-a-string-to-an-enum-value-in-vb-net) – Nalaka526 Aug 15 '14 at 06:46

3 Answers3

1

Try this piece of code.

 Foo = CType(System.Enum.Parse(GetType(Cards), "club"), Cards)
JammoD
  • 419
  • 5
  • 15
1

I'd use TryParse as shown below. The value returned is enum type, but the type is an integer and can be used as such.

Enum Cards
    club = 1
    spade = 2
    hearts = 3
    diamond = 4
End Enum

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim enumVlaue As Cards
    If Not [Enum].TryParse("spade", enumVlaue) Then
        'did not convert
        Stop
    End If

    Dim foo As Integer = enumVlaue + 42
End Sub
dbasnett
  • 11,334
  • 2
  • 25
  • 33
-3

If it's an integer value, try:

enumVariable * 1
Zoe
  • 27,060
  • 21
  • 118
  • 148