7 Oct 2009

Enums / Data Binding

I use enums in my code a lot.
Often I have to set the various enumerations into the text in a text box. This is simple:

Me.ComboBoxGameCode.DataSource = System.Enum.GetValues(GetType(NumbersGameDraw.GameType))

This is nice but not great.
Say we have enums representing multiple words (for example the following)
Paid
NotPaid (we would like NotPaid to display as Not Paid).

So I created enumhelper class to do this:

Public Class EnumHelper
Private Shared Function ChangeHungarianTextToSeperateWords(ByVal originalText As String) As String

Dim strOutput As String = ""
Dim i As Integer = 0
While i < originalText.Length
Dim char1 As Char
char1 = originalText.Substring(i, 1)

If Char.IsUpper(char1) Then
strOutput &= " " & char1
Else
strOutput &= char1
End If
i += 1
End While
Return strOutput.Trim

End Function

Public Shared Function GetEnumSeperateWordsList(ByVal enumType As Type) As ArrayList
Dim x = System.Enum.GetNames(enumType)
Dim y As New ArrayList

For Each item1 As Object In x
'item1 = Me.ChangeHungarianTextToSeperateWords(item1)
y.Add(EnumHelper.ChangeHungarianTextToSeperateWords(item1))
Next
Return y

End Function
End Class

No comments:

Post a Comment