Enum but with values?

U

UJ

I know how to define enums, and they work great.

But is there any way to define the same time of thing but give them actual
values?

For instance, I'd like to define something with the idea like this:

Public Enum TMCContentStatus as string
PrevApproved = "P"
ToBeAdded = "A"
ToBeDeleted = "D"
AddDenied = "X"
DeleteDenied = "Y"
Approved = " "
End Enum

This way, I can then later do the folllowing command and it would be true:

dim junk as TMCContentStatus
junk = TMCContentStatus.PrevApproved
if ( junk = "P" ) then....

I know I could do this right now with enums but I need to have specific
values to save into a database.

Any ideas on how to do this?

TIA

Jeffrey.
 
M

Mattias Sjögren

But is there any way to define the same time of thing but give them actual
values?

Are you saying that the numerical values you can have in an enum
aren't "actual values"?

For instance, I'd like to define something with the idea like this:

Public Enum TMCContentStatus as string
PrevApproved = "P"
ToBeAdded = "A"
ToBeDeleted = "D"
AddDenied = "X"
DeleteDenied = "Y"
Approved = " "
End Enum

Public Class TMCContentStatus
Public Const PrevApproved As String = "P"
.... and so on ...
End Module



Mattias
 
H

Herfried K. Wagner [MVP]

UJ said:
I know how to define enums, and they work great.

But is there any way to define the same time of thing but give them actual
values?

For instance, I'd like to define something with the idea like this:

Public Enum TMCContentStatus as string
PrevApproved = "P"
ToBeAdded = "A"
ToBeDeleted = "D"
AddDenied = "X"
DeleteDenied = "Y"
Approved = " "
End Enum

This way, I can then later do the folllowing command and it would be true:

Creating "enumerations" of any data type
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=anytypeenums&lang=en>
 
Q

Qwert

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(ByVal 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.GetNames(enuDummy.GetType)
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.GetValues(enuDummy.GetType)
REM Search for the name.
For i = 0 To colNames.GetLength(0) - 1
If colNames.GetValue(i) = strString Then
Return colValues.GetValue(i)
End If
Next
REM String not found.
Return enuDummy

End Function



Example:

Public Enum TMCContentStatus as Integer
Undefined
PrevApproved
ToBeAdded
ToBeDeleted
AddDenied
DeleteDenied
Approved
End Enum

Dim junk as TMCContentStatus
strTemp = ....get string that represents enumeration value from database.
junk = StringToEnum( strTemp, TMCContentStatus.Undefined )

Turning an enumeration value into a string is easy, just add '.ToString' at
the end.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top