Enum.TryParse(TEnum) converts string to enumerated object : Enum « Data Types « VB.Net






Enum.TryParse(TEnum) converts string to enumerated object

 

<Flags> Enum Colors As Integer
   None = 0
   Red = 1
   Green = 2
   Blue = 4
End Enum

Module Example
   Public Sub Main()
      Dim colorStrings() As String = {"0", "2", "8", "blue", "Blue", "Yellow"}
      For Each colorString As String In colorStrings
         Dim colorValue As Colors
         If [Enum].TryParse(colorString, colorValue) Then        
            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                    
         Else
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString)
         End If
      Next
   End Sub
End Module

   
  








Related examples in the same category

1.Convert Enum element to int and call ToStringConvert Enum element to int and call ToString
2.Convert Element in Enum to IntegerConvert Element in Enum to Integer
3.Enum TemperaturesEnum Temperatures
4.Convert Enum to IntConvert Enum to Int
5.Use Enum to define Access LevelUse Enum to define Access Level
6.Enum data DemoEnum data Demo
7.Enum Class provides the base class for enumerations.
8.Using type to convert integer to Enum
9.Convert Enum value back and forth
10.Check if the backend value is defined for an Enum
11.Get Enum underline type
12.Enum parse String value to Enum
13.Use TryParse to parse string value to Enum
14.Get Enum value names
15.Get Enum values
16.Flag Enum values
17.Or operation on Flag enum value
18.And Operation on Flag enum value
19.Equals comparison for Enum value
20.The days of the week, and their corresponding values in the Days Enum
21.Output the enum underline values
22.Combine the flag enum values
23.Enum.CompareTo compares this instance to a specified object and returns an indication of their relative values.
24.Enum.Equals Method returns a value indicating whether this instance is equal to a specified object.
25.Enum.GetName Method retrieves the name of the constant in the specified enumeration that has the specified value.
26.Enum.GetNames Method retrieves an array of the names of the constants in a specified enumeration.
27.Enum.GetValues Method retrieves an array of the values of the constants in a specified enumeration.
28.Enum.HasFlag Method tells whether one or more bit fields are set in the current instance.
29.Check flag Enum combination
30.Enum.IsDefined Method tells whether a constant with a specified value exists in a specified enumeration.
31.Call IsDefined with invalid underlying integral value.
32.Call IsDefined with string containing member name.
33.Call IsDefined with a variable of type PetType.
34.Call IsDefined with uppercase member name.
35.Call IsDefined with combined value
36.Enum.Parse Method converts string to an enumerated object.
37.Enum.Parse Method converts string to enumerated object
38.Sample for Enum.ToString(String)
39.Enum.ToString Method converts the value to string representation.
40.Convert Enum value to string with ToString method
41.Enum.TryParse(TEnum) converts string to enumerated object
42.Enum value ToString: G F D X
43.FlagsAttribute Indicates that an enumeration can be treated as a bit field; that is, a set of flags.