In writing a Silverlight 3 beta 1 application, I wanted to define a series of Enums and have a method that would return a complete list of the enumeration values, both the text and the numeric value, for use in binding and/or database fields. I have written similar routines in Winforms without issue.
I was disappointed to find that the Silverlight SDK supports only the Equals, GetName, GetUnderlyingType, IsDefined, Parse, ReferenceEquals, and ToObject methods.
You will notice the absence of the GetNames() and GetValues() methods that would simplify creating my desired list.
I was aware that the Silverlight runtime is a subset of the full .Net framework, but this is the first, but probably not the last, time that it bit me.
To get around this limitation, I had to add a bit more code in my enums.vb file. I had to explicitly create a structure to hold the enum's name and value, then create an additional routine for each enum that returns a List of that structure.
The following code shows the final result for one enum, Visibility. Each additional enum would require another Region similar to the Visibility region.
If anyone has a suggestion for more efficient code considering the limitations, I would be very happy to hear it. Just leave a comment with your suggestion.
Hope that helps!
Microsoft MVP VB
Okemos, MI USA
Public Class enums
Public Structure EnumValue
Dim Name As String
Dim Value As Integer
Public Sub New(ByVal _Name As String, ByVal _Value As Integer)
Name = _Name
Value = _Value
Public Enum Visibility As Integer
'Remember to change the associated List function too
NotSet = 0
Collapsed = 2
Visible = 4
Public Shared Function VisibilityList(ByVal LanguageName As Language) _
As List(Of EnumValue)
'Required because Silverlight runtime does not have the GetNames()
Dim Result As New List(Of EnumValue)
Result.Add(New EnumValue("NotSet", 0))
Result.Add(New EnumValue("Collapsed", 2))
Result.Add(New EnumValue("Visible", 4))