1

It's very easy I have the following Enum

Public Enum TCheckStatus
        Checked
        NotChecked
        Indeterminate 
    End Enum

I want to get the Index of the given name found in the enum, what's the best way?!!

For Example:

Suppose I have "NotChecked" string , I want the output to be 1

Wael Dalloul
  • 22,172
  • 11
  • 48
  • 57
  • What is a given name found in the enum? – Tim Schmelter Jan 04 '12 at 09:22
  • Suppose I have "NotChecked" string , I want the output to be 1 – Wael Dalloul Jan 04 '12 at 09:29
  • 1
    Do you want the index or the value? (They might not be the same if the enum has been declared with explicit values, and I can't think of any good reason why you would need the index rather than the value. Relying on the index rather than the value would be very brittle.) – LukeH Jan 04 '12 at 11:12
  • @LukeH Yes you are right, it's better to use the value but I miss the right words to explain my issue. – Wael Dalloul Jan 04 '12 at 11:51

5 Answers5

2

I can't give precise vb syntax, but I'll write it in C# so see if you can use it:

int index = (int)Enum.Parse( typeof(TCheckStatus), "NotChecked");

index is 1 in this case. If you give an invalid string (not a member of the enum), it will throw an exception. If this is not what you want, you can use Enum.TryParse instead.

If you already have the enum, you can just cast it to a int to get the index:

var myEnum = TCheckStatus.NotChecked;
int index = (int)myEnum;
Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
  • 2
    Note that if the enum has been declared with explicit values then `(int)myEnum` will give you the *value*, not necessarily the index. Using the index is very brittle and not a good idea, since a dev might legitimately assume that the members can safely be re-ordered as long as they keep their values. – LukeH Jan 04 '12 at 11:12
  • When I said index I really meant value, but good that you made it clear for everyone. – Øyvind Bråthen Jan 04 '12 at 13:02
2

You are looking for Enum.GetNames. It retrieves an array of the names of the constants in a specified enumeration.

Dim notCheckedStatus  = DirectCast([Enum].Parse(GetType(TCheckStatus), "NotChecked"), TCheckStatus)
Dim allStatusNames    = [Enum].GetNames(GetType(TCheckStatus))
Dim indexOfNotChecked = Array.IndexOf(AllStatusNames, "NotChecked") '=1'
  1. line: parses a string variable to an enum-value
  2. line: returns all enum-values of a given enum-type as Array
  3. line: returns the index of a given enum-value in that Array

Of course to solve your question you only need this:

Array.IndexOf([Enum].GetNames(GetType(TCheckStatus)), "NotChecked") 
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • This answer returns the Index, in some scenarios it can differ from the Value. For example an Enum that start at value 1 or when something got deleted or rearranged. The Value can be retrieved by following vb.net code: `CInt(System.Enum.Parse(GetType(TCheckStatus), "NotChecked"))`. – T_D Mar 19 '15 at 10:22
1

To get arrays of the names and their integer equivalents with the Enum methods in VB.net, please use GetNames() and GetValues() method. Use Parse() to get the index. Hope this helps.

Anil
  • 967
  • 10
  • 20
1
int Output = (int)Enum.Parse(typeof(TCheckStatus), "NotChecked");
1

try this:

Dim val as integer = cint(DirectCast([Enum].Parse(GetType(TCheckStatus), _
                          "NotChecked"), TCheckStatus)

StackOverflow - Parse a string to an Enum value in VB.NET

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492