By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,247 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,247 IT Pros & Developers. It's quick & easy.

Enumerated Values With Initializers

P: n/a
I'm using an enumerated type to identify a record type that relates to a
unique ID in a database. Identity columns in SQL start at 1, while
enumerated types in C# start at 0. I didn't think it would be a big
deal because I could just force the values of the enum to start at 1.

Database:

ID RecordType
-- ----------
1 TypeA
2 TypeB
3 TypeC
4 TypeD

C#:

enum RecordType
{
TypeA = 1,
TypeB,
TypeC,
TypeD,
}

The problem is that when the enumerated type is serialized as XML, the
initialization is dropped and on the client side, C# again starts the
values at 0. This wouldn't be a big deal if my web method was returning
a collection of strongly typed classes; however, it's returning a
DataSet. So when I read the value of the column from the database and
compare it to the value of the enumerated type, I end up with an "off by
one" error.

I'd rather not force the client into remembering to always add 1 to the
enumerated values, but the only alternative I can think of is to add a
"none" record type to the RecordType enum because I'm afraid that
somebody might think that "none" is a desirable value and then the
problem would be pushed onto the database side.

Is there any way to make certain that the web service serializes the
enum with the correct values?

Thank you in advance,

--
Sean

website: http://senfo.blogspot.com
Feb 8 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"senfo" <en**********@yahoo.comI-WANT-NO-SPAMwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
I'm using an enumerated type to identify a record type that relates to a
unique ID in a database. Identity columns in SQL start at 1, while
enumerated types in C# start at 0. I didn't think it would be a big deal
because I could just force the values of the enum to start at 1.

Database:

ID RecordType
-- ----------
1 TypeA
2 TypeB
3 TypeC
4 TypeD

C#:

enum RecordType
{
TypeA = 1,
TypeB,
TypeC,
TypeD,
}
enums don't really map to XML schema, so they don't really map to WSDL.

You might think that enums are supported, because there's an xs:enumeration
facet in XML schema:

<xs:simpleType name="Risk">
<xs:restriction base="xs:int">
<xs:enumeration value="1"/>
<xs:enumeration value="2"/>
<xs:enumeration value="3"/>
</xs:restriction>
</xs:simpleType>

Unfortunately, this is only restricting the lexical space of the Risk type.
It only indirectly restricts the possible values. Another example:

<xs:simpleType name="Risk">
<xs:restriction base="xs:string">
<xs:enumeration value="Low"/>
<xs:enumeration value="Medium"/>
<xs:enumeration value="High"/>
</xs:restriction>
</xs:simpleType>

This restricts values of this type to one of the three strings, but there is
no association between, for instance, "Medium" and the number 2.

Since XML Schema does not have the semantics you're looking for, you should
stick to something it _can_ describe, like integers.

John
Feb 8 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.