Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test for String = Enum.Value?

Tags:

enums

vb.net

How do I do a simple compare of an enum value and a string that should match the enums name?

How do I parse the string into it's appropriate enum value.

For example,

Enum A
     B=0
     C=1
     D=2
End Enum

How do I check if String = A.C and how do I convert string into its corresponding A value without comparing it to a string representation?

like image 455
Middletone Avatar asked Jan 25 '09 00:01

Middletone


People also ask

How do you check if a string is present in enum?

toList()). contains(aString); EventNames is the name of the enum while getEvent() is what returns the associated string value of each enum member.

Can you use == for enums?

Because there is only one instance of each enum constant, it is permissible to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant.


4 Answers

There are several different methods that are related:

Enum.GetName(typeof(A), A.C) == "C"
A.C.ToString() == "C"
((A)Enum.Parse(typeof(A), "C")) == A.C

The first two convert the value of A.C to a string representation ("C") and then compare it to a string. The last one converts the string "C" to a type A, and then compares as an actual type A.

Enum to string: enumValue.ToString() or Enum.GetName(typeof(A), A.C)

String to enum: (A)Enum.Parse(typeof(A), "C")

Note that none of those will really work if the enumeration is marked with FlagsAttribute.

like image 100
earlNameless Avatar answered Oct 19 '22 11:10

earlNameless


The Enum.Parse method:

Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. A parameter specifies whether the operation is case-sensitive.

Here's the VB.NET example code from MSDN:

Module Example
   Public Sub Main()
      Dim colorStrings() As String = {"0", "2", "8", "blue", "Blue", "Yellow", "Red, Green"}
      For Each colorString As String In colorStrings
         Try
            Dim colorValue As Colors = CType([Enum].Parse(GetType(Colors), colorString, True), Colors)        
            If [Enum].IsDefined(GetType(Colors), colorValue) Or colorValue.ToString().Contains(",") Then 
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString())
            Else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString)            
            End If                    
         Catch e As ArgumentException
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString)
         End Try
      Next
   End Sub
End Module
like image 29
Mitch Wheat Avatar answered Oct 19 '22 11:10

Mitch Wheat


Enum.GetName(typeof(A),enumValue)==stringValue

like image 40
JoshBerke Avatar answered Oct 19 '22 11:10

JoshBerke


You can also use the name() function to check this

A.C.name() == "C"
like image 2
Kurru Avatar answered Oct 19 '22 09:10

Kurru