Enumerated Types

Discussion in 'ASP .Net Web Services' started by Joe, Oct 6, 2003.

  1. Joe

    Joe Guest

    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
     
    Joe, Oct 6, 2003
    #1
    1. Advertisements

  2. 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
     
    Brooks Stoner [MSFT], Oct 9, 2003
    #2
    1. Advertisements

  3. Joe

    WFB Guest

    Brooks,

    Thanks a lot for your help - this works great.

    Joe


     
    WFB, Oct 10, 2003
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.