Problem with casting integer values to enum

M

mitdej

Hi there,
I have an several enum types that starts from a nunmber other than 0.
For example:
public enum InternalStatus
{
Pending = 1,
Ported = 2,
Suspended = 3
}
I put this values in a int column of a MSSQL table.

When I try to read tha values back from the database and cast it to
the enuum type, the casting acts as the enum type starts from 0.

I tried Enum.Parse, but it didn't help.

Any suggestion...?
 
J

Jon Skeet [C# MVP]

I have an several enum types that starts from a nunmber other than 0.
For example:
public enum InternalStatus
{
Pending = 1,
Ported = 2,
Suspended = 3
}
I put this values in a int column of a MSSQL table.

When I try to read tha values back from the database and cast it to
the enuum type, the casting acts as the enum type starts from 0.

I tried Enum.Parse, but it didn't help.

Any suggestion...?

That sounds very unlikely to me. Could you post a short but complete
example showing the problem? The database side should be irrelevant,
as by the time you're casting you should already have the value as an
integer.

Jon
 
M

mitdej

This is the problem code. Now that I look closely to the code it is
the opposite situation. Converting the enumeration to int value.

psn.RemoveSent((int)PortingNotification_WS.CDBMessageType.PortingFromDonor_Notification
+1, Convert.ToInt32(dr["OperatorID"]), fromDate, toDate))

This is called from a Client App which is calling a web service and
the enumeration is trransferred as part of the proxy class.

The enumeration is on the server side and is defined as:

public enum CDBMessageType
{
SuspensionOfService_Notification = 1,
TerminateSuspension_Notification = 2,
Disconnection_Notification = 3,
RoutingErrors_Notification = 4,
PortingFromDonor_Notification = 5,
PortAccross_Notification = 6,
PortingAcceptance_Notification = 7,
PortingRejection_Notification = 8,
CancelPorting_Notification = 9,
ChangeInAssignedSeriesOfSubscriberNumbers_Notification = 10
}

As You can see I have to add 1 to the value of the enumeration so it
can be casted to the correct int.
I think that WSDL doesn't transfer the information of the values of
the enuumeration types.
Any smarter solution?
 
J

Jon Skeet [C# MVP]

This is called from a Client App which is calling a web service and
the enumeration is trransferred as part of the proxy class.

That may well be the problem. Have you tried looking at the XML
responses with something like Fiddler?

Jon
 
M

mitdej

I haven't heard about fiddler :)

this is the enumeration as is generated in the proxy class:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml",
"2.0.50727.832")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://
tempuri.org/")]
public enum CDBMessageType {

/// <remarks/>
SuspensionOfService_Notification,

/// <remarks/>
TerminateSuspension_Notification,

/// <remarks/>
Disconnection_Notification,

/// <remarks/>
RoutingErrors_Notification,

/// <remarks/>
PortingFromDonor_Notification,

/// <remarks/>
PortAccross_Notification,

/// <remarks/>
PortingAcceptance_Notification,

/// <remarks/>
PortingRejection_Notification,

/// <remarks/>
CancelPorting_Notification,

/// <remarks/>
ChangeInAssignedSeriesOfSubscriberNumbers_Notification,
}

there are no assigned int values to the enumeration values, so I Guess
this is the problem.
Maybe Microsoft team should be informed about this.

Anyway thank You for Your quick responses to my posts.
 

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