Get Enum Value from String
February 20, 2008
Here is a quick tip. If you have the following Enum
Enum Frequencies DAILY WEEKLY MONTHLY End Enum
And you want to get the enum value from a string, you can:
Dim myString as String = "WEEKLY" Dim val as Frequencies = _System.Enum.Parse(GetType(Frequencies), myString)
If you try to parse a string that doesn’t exist e.g. "YEARLY", an ArgumentException exception is thrown with a message "Requested value ‘YEARLY’ was not found".
Also note that the string is case-sensitive, so "Weekly" will also throw the same exception.
Posted in

content rss

Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks