code_junkie

How to test for String = Enum.Value

Nov 13th, 2011
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. Enum A
  2. B=0
  3. C=1
  4. D=2
  5. End Enum
  6.  
  7. Enum.GetName(typeof(A), A.C) == "C"
  8. A.C.ToString() == "C"
  9. ((A)Enum.Parse(typeof(A), "C")) == A.C
  10.  
  11. Module Example
  12. Public Sub Main()
  13. Dim colorStrings() As String = {"0", "2", "8", "blue", "Blue", "Yellow", "Red, Green"}
  14. For Each colorString As String In colorStrings
  15. Try
  16. Dim colorValue As Colors = CType([Enum].Parse(GetType(Colors), colorString, True), Colors)
  17. If [Enum].IsDefined(GetType(Colors), colorValue) Or colorValue.ToString().Contains(",") Then
  18. Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString())
  19. Else
  20. Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString)
  21. End If
  22. Catch e As ArgumentException
  23. Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString)
  24. End Try
  25. Next
  26. End Sub
  27. End Module
  28.  
  29. A.C.name() == "C"
Add Comment
Please, Sign In to add comment