> I know I could do this right now with enums but I need to have specific
values to save into a database.
Save enumerations as string in the database. This makes the database easier
to read then storing it as a value. Then use the following function to
convert the string into the enumation value:
Public Shared Function StringToEnum(By Val strString As String, ByVal
enuDummy As [Enum]) As [Enum]
REM Search for an enumeration member with the name strString.
REM enuDummy is a member of the enumeration to be searched.
REM When strString is found, that enumeration member is returned.
REM When strString is invalid or not found, enuDummy is returned.
REM When enuDummy is Nothing, an Exception is thrown.
Dim colNames As Array
Dim colValues As Array
Dim i As Integer
REM Check parameters.
If enuDummy Is Nothing Then
Throw New Exception("The enumeration object is 'Nothing'. This
is not allowed.")
End If
If strString Is Nothing Then
Return enuDummy
End If
If strString Is "" Then
Return enuDummy
End If
REM A string array of the names of the constants in enumType. The
elements of the
REM array are sorted by the values of the enumerated constants.
colNames = enuDummy.GetNam es(enuDummy.Get Type)
REM An Array of the values of the constants in enumType. The
elements of the array
REM are sorted by the values of the enumeration constants.
colValues = enuDummy.GetVal ues(enuDummy.Ge tType)
REM Search for the name.
For i = 0 To colNames.GetLen gth(0) - 1
If colNames.GetVal ue(i) = strString Then
Return colValues.GetVa lue(i)
End If
Next
REM String not found.
Return enuDummy
End Function
Example:
Public Enum TMCContentStatu s as Integer
Undefined
PrevApproved
ToBeAdded
ToBeDeleted
AddDenied
DeleteDenied
Approved
End Enum
Dim junk as TMCContentStatu s
strTemp = ....get string that represents enumeration value from database.
junk = StringToEnum( strTemp, TMCContentStatu s.Undefined )
Turning an enumeration value into a string is easy, just add '.ToString' at
the end.