QA: Using Enum.Parse to Convert a String to an Enumeration Value

Question:
I have a string that holds the name of a value from an enumeration. How do I “cast“ that to the actual enumeration type?

Answer:
You can use the Parse method of the Enum class to do this conversion for you. Some sample VB code is shown below.

Dim dr As DialogResult
dr = CType([Enum].Parse(GetType(DialogResult), "Cancel"), DialogResult)
MsgBox(dr.ToString)

Published Fri, Jul 9 2004 0:28 by windsor
Filed under:

Comments

# re: QA: Using Enum.Parse to Convert a String to an Enumeration Value

Thanks - this works well.

Thursday, December 01, 2005 1:51 AM by windsor