473,805 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1615
"senfo" <en**********@y ahoo.comI-WANT-NO-SPAMwrote in message
news:%2******** *******@TK2MSFT NGP05.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:simpleTyp e name="Risk">
<xs:restricti on base="xs:int">
<xs:enumerati on value="1"/>
<xs:enumerati on value="2"/>
<xs:enumerati on 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:simpleTyp e name="Risk">
<xs:restricti on base="xs:string ">
<xs:enumerati on value="Low"/>
<xs:enumerati on value="Medium"/>
<xs:enumerati on 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
2211
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 declarations like so: <!ATTLIST MyElement MyAttribute (One | Two | Three) "One" > The VB code needs to generate dynamic pick-list for all the attributes that
0
1079
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 components. One of these shared items is a bunch of commonly used types. These included hard-coded paths, extension types, and some common enumerated values. This 'types.cs' file does not implement the MarshalByRefObject.
2
1285
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
2685
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 = DayOfWeek.Wednesday
22
2142
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. This raises questions. Is there any difference between a Python immutable value, and a constant? I suppose "constant" also implies that the *name* binds
4
4388
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 machines in C# where the machine can use the enumerated 'state' to execute (thus the term 'machine' as in 'state machine') the machine operations as this requires boolean operations to be performed on the state. .....
7
2629
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 go about checking that a supplied integer is contained within the set of values of an enumeration? Is there a way to do this? Thanks,
11
5835
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 but returns 4 twice (the number of bytes used to store a value in test) when the needed answer is 5 (the number of enumerated values in atype). #include <stdlib.h> #include <stdio.h>
1
7337
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 programmer knows about the constructor he/she feels they have found a good place to put code that must be run before an object is created and sometimes don't try to look for more places to put pre-object creation code. The other places to put...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10609
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10360
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5542
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.