Creating an Enum that has specific Integer values across web service

Administrator

VB.NET Forum Admin
Joined
Jun 3, 2004
Messages
1,462
Programming Experience
10+
I want to use a custom enum of type Integer with set values to each enum specified. For example, I have the following Enum in a referenced DLL the web service uses:

VB.NET:
<Serializable()> _
Public Enum enuCommandType As Integer
Text = 1
StoredProcedure = 4
End Enum

But when the Reference.vb is created, it's not of specific type Integer and therefore the values get messed up as they are numbered 0, 1, in order:

VB.NET:
'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.312"), _
System.SerializableAttribute(), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://xxxxxxxxxxxxxxxxDELETEDXXXXXX/")> _
Public Enum enuCommandType

'''<remarks/>
Text

'''<remarks/>
StoredProcedure
End Enum

How do you create an Enum for use with web services that will retain the exact Integer assignments?
 
The correct enum value is always returned.

The integer representation of the enum value is a matter of its own. Webservice consumer will re-number the enum privately according to whether it is a regular enum or Flags enum. For regular enum each member will be numbered 0,1,2,3,4 etc, for Flags enum each member will be numbered 1,2,4,8 etc.

Normally the Integer value of an enum does not matter for the user. I can't see any situations where the re-numbering as described above will pose a problem if the Enum is used correctly.

Example webservice (I turned on/off the Flags atttribute)
VB.NET:
'<Flags()> _
Public Enum wsEnum
en0 = 0
en1 = 1
en2 = 2
en3 = 4
en4 = 8
End Enum
 
<WebMethod()> _
Public Function Hello2() As wsEnum
Return wsEnum.en3
End Function
Example consumer:
VB.NET:
Dim l As New localhost.Service
Dim en As localhost.wsEnum = l.Hello2
MsgBox(en.ToString)
 
JohnH,

As you can see, I had to specifically number (integer assign) my custom enum to match that of the System.Data enums that it will translate to. So I have an enum with two values, 1 and 4, but when passed across the web service, the integer assignments are not bound, so instead we get a 0 and 1 assignment, which doesn't CType to the system.data enum that it will need to translate back to.
 
Oh, I didn't make that connection (pun). I that case you should make the type CommandType and not your redefined enum. You will now see something peculiar, when consuming this service you will see the type is changed from System.Data.CommandType to serviceNamespace.CommandType, ie the connection is broken and the service claim this to be one of its own public types. What needs to be done is then to catch the value as a service type and use the Enum.Parse method to transfer the enum value back to System.Data.CommandType value.

Here is the modified code examples, web service:
VB.NET:
<WebMethod()> _
Public Function Hello3() As [COLOR=darkgreen][B]System.Data.CommandType[/B][/COLOR]
Return Data.CommandType.StoredProcedure
End Function
consumer:
VB.NET:
Dim l As New localhost.Service
Dim ct As localhost.CommandType = l.Hello3 '(the true return type)
Dim ctreal As System.Data.CommandType = System.Enum.[B][COLOR=darkgreen]Parse[/COLOR][/B](GetType(CommandType), ct.ToString)
MsgBox(ctreal.ToString)
 
Thanks JohnH. Yeah, that was my next step, to take the text value and retrieve the assignment back out of the strongly typed enum. I have been fiddling with my own enums vs. System.Data supplied enums, not knowing was was serializable or not, i.e. usable across the wire. Amazing how many hours are wasted working with these quirks!
 
Back
Top