Enumerated Types

J

Joe

Hi,

In my web service I have a enumerated type as follows

Enum ProductType as Integer
Household = 765
Outdoor = 9
Plumbing = 129
Electrical = 239
End Enum

But on the client it is always translated such that Household = 0, Outdoor =
1, etc. Is there an attribute I should be flagging my type with to have
these values persist or am I missing something obvious?

Thanks
Joe
 
B

Brooks Stoner [MSFT]

Hi Joe,

This is a result of the Xml serialization when you generate a new proxy in
the client to connect to the web service. I went through a number of
scenarios with enumerations in web services, and found the same behavior.

If you pass the enum to the web service, it will match up correctly, and you
can then use the underlying value in the web service. So if you never need
the underlying values in your client code, you don't have to do anything
different, and can just use the enumerations.

If, however, you need to have the underlying values in your client app, you
will need to create a value mapping on the client. Here is a way to create
the mapping in a way that you can get the int value by referencing the
mapping object using the enumeration as the index.

public class ProductTypeMapping
{
private static HybridDictionary m_productTypes;
public static HybridDictionary ProductTypes
{
get { return m_productTypes; }
}
private ProductTypeMapping()
{
}
static ProductTypeMapping()
{
m_productTypes= new HybridDictionary();
m_productTypes.Add(ProductType.Household, 765);
m_productTypes.Add(ProductType.Outdoor, 9);
m_productTypes.Add(ProductType.Plumbing, 129);
m_productTypes.Add(ProductType.Electrical, 239);
}
public static int Map(ProductType index)
{
return (int) m_productTypes[index];
}
}

Then, if you need the value, you can get it as follows:

ProductType myProduct = new ProductType();
myProduct = ProductType.Outdoor;
int myProductValue = ProductTypeMapping.Map(myProduct);

I hope this helps.

Brooks Stoner
 
W

WFB

Brooks,

Thanks a lot for your help - this works great.

Joe


Brooks Stoner said:
Hi Joe,

This is a result of the Xml serialization when you generate a new proxy in
the client to connect to the web service. I went through a number of
scenarios with enumerations in web services, and found the same behavior.

If you pass the enum to the web service, it will match up correctly, and you
can then use the underlying value in the web service. So if you never need
the underlying values in your client code, you don't have to do anything
different, and can just use the enumerations.

If, however, you need to have the underlying values in your client app, you
will need to create a value mapping on the client. Here is a way to create
the mapping in a way that you can get the int value by referencing the
mapping object using the enumeration as the index.

public class ProductTypeMapping
{
private static HybridDictionary m_productTypes;
public static HybridDictionary ProductTypes
{
get { return m_productTypes; }
}
private ProductTypeMapping()
{
}
static ProductTypeMapping()
{
m_productTypes= new HybridDictionary();
m_productTypes.Add(ProductType.Household, 765);
m_productTypes.Add(ProductType.Outdoor, 9);
m_productTypes.Add(ProductType.Plumbing, 129);
m_productTypes.Add(ProductType.Electrical, 239);
}
public static int Map(ProductType index)
{
return (int) m_productTypes[index];
}
}

Then, if you need the value, you can get it as follows:

ProductType myProduct = new ProductType();
myProduct = ProductType.Outdoor;
int myProductValue = ProductTypeMapping.Map(myProduct);

I hope this helps.

Brooks Stoner
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.

Joe said:
Hi,

In my web service I have a enumerated type as follows

Enum ProductType as Integer
Household = 765
Outdoor = 9
Plumbing = 129
Electrical = 239
End Enum

But on the client it is always translated such that Household = 0,
Outdoor
=
1, etc. Is there an attribute I should be flagging my type with to have
these values persist or am I missing something obvious?

Thanks
Joe
 

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

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top