+1 to Ani. Here's the VB.NET version
Here's the VB.NET version of Ani's answer:
Public Enum typFoo
itemA = 1
itemB = 2
itemC = 3
End Enum
Sub example()
Dim dict As Dictionary(Of Integer, String) = System.Enum.GetValues(GetType(typFoo)) _
.Cast(Of typFoo)() _
.ToDictionary(Function(t) Integer.Parse(t), Function(t) t.ToString())
For Each i As KeyValuePair(Of Integer, String) In dict
MsgBox(String.Format("Key: {0}, Value: {1}", i.Key, i.Value))
Next
End Sub
Additional example
In my case, I wanted to save the path of important directories and store them in my web.config
file's AppSettings section. Then I created an enum to represent the keys for these AppSettings...but my front-end engineer needed access to these locations in our external JavaScript files. So, I created the following code-block and placed it in our primary master page. Now, each new Enum item will auto-create a corresponding JavaScript variable. Here's my code block:
<script type="text/javascript">
var rootDirectory = '<%= ResolveUrl("~/")%>';
// This next part will loop through the public enumeration of App_Directory and create a corresponding JavaScript variable that contains the directory URL from the web.config.
<% Dim App_Directories As Dictionary(Of String, App_Directory) = System.Enum.GetValues(GetType(App_Directory)) _
.Cast(Of App_Directory)() _
.ToDictionary(Of String)(Function(dir) dir.ToString)%>
<% For Each i As KeyValuePair(Of String, App_Directory) In App_Directories%>
<% Response.Write(String.Format("var {0} = '{1}';", i.Key, ResolveUrl(ConfigurationManager.AppSettings(i.Value))))%>
<% next i %>
</script>
NOTE: In this example, I used the name of the enum as the key (not the int value).