473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Enumerated Values With Initializers

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
1 1595
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alastair Cameron | last post by:
I have a VB (VB6, not .NET) application that reads an XML file (using MSXML v3.2 parser); the XML file contains a reference to an external DTD. The DTD has numerous enumerated attribute...
0
by: Brandon | last post by:
Hello all, and thanks for taking a look at this. First off, the project I am working on is a server/client application that contains a shared assembly of common classes between the two...
2
by: Daniel Wilson | last post by:
I have an enumerated type like this: public __value enum MyType{ ABC = 1, DEF = 2, GHI = 3, JKL = 4 }; Later I have a for loop like this: for (ft = MyType::ABC; ft <= MyType::JKL; ft++){
5
by: Rob | last post by:
Hi, I have defined an enumeration thus: Public Enum CollectionDayOfWeek NoCollectionDay = -1 Sunday = DayOfWeek.Sunday Monday = DayOfWeek.Monday Tuesday = DayOfWeek.Tuesday Wednesday =...
22
by: Ben Finney | last post by:
Howdy all, I've recently packaged 'enum' in PyPI. In its description, I make the claim that it creates "immutable" enumeration objects, and that the enumeration values are "constant" values. ...
4
by: Shawnk | last post by:
This post is intended to verify that true value semantics DO NOT EXIST for the Enum class (relative to boolean operations). If this is true then (thus and therefore) you can not design state...
7
by: John Goche | last post by:
Hello, The following program compiler and runs fine under gcc and produces the output 3. However, I am not sure whether such behavior is legal. In particular, as a related question, how would I...
11
by: David Mathog | last post by:
Is there a standard compliant method to access the number of elements in an enumerated type in C, either from within the preprocessor or the running program? The example below compiles and runs...
1
by: r035198x | last post by:
Inspiration Inspired by a post by Jos in the Java forum, I have put together a little article on class initializers. Initializers are indeed underutilized by most Java programmers. Once the Java...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.