Link to home
Start Free TrialLog in
Avatar of zorba111

asked on

WCF enums not functioning like "vanilla" enums

Ok, so to confirm the behaviour of "vanilla" enums, I wrote this test code:

    enum EmpType

// in Main() or a method somehwere....

            EmpType emp = (EmpType)10;
            Boolean bIsDefined = Enum.IsDefined(typeof(EmpType), emp);

            emp = (EmpType)1;
            bIsDefined = Enum.IsDefined(typeof(EmpType), emp);

Open in new window

As you'd expect, the first call to Enum.IsDefined() returns false, and the second returns true (as 10 is *not* in the enum, but 1 *is*).

Ok, but this behaviour is not working in my WCF client, using a WCF-generated enum:

// definition of the enum "JobLevelIdEnum" in the generated reference.cs file:

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.5483")]
    public enum JobLevelIdEnum {
        /// <remarks/>
        /// <remarks/>
        /// <remarks/>
        /// <remarks/>
        /// <remarks/>
        /// <remarks/>
        /// <remarks/>

// then in my code, we get a value from a combo-box, which I want to confirm is A VALID MEMBER of the enum.... 
//  otherwise we can use the DEFAULT member of the enum... 

            // O) job-info -> level
            // we get a string from the combo box, but monster needs an enum (JobLevelIdEnum)
            JobLevelEnumStr jobLevel = new JobLevelEnumStr();
            Int32 iLevel = (Int32.Parse(vac.MONSTERlevel));
            JobLevelIdEnum level = (JobLevelIdEnum)iLevel;

            // next, check that the number returned from the combobox is in the actual enum range
            if (Enum.IsDefined(typeof(JobLevelIdEnum), level))
                jobLevel.monsterId = level;
                jobLevel.monsterId = JobLevelIdEnum.Item16;  // which is the "none of these" option

Open in new window

The problem is, in the code above, that even when the combo-box is returning "valid" values, (10-16, inclusive), the Enum.IsDefined() call is returning false....

Why is the WCF enum acting differently? It must be all the macros and attributes. But why?
It would be much easier to work with if WCF enums allowed us to distinguish between valid enums in range and not in range.

********** UPDATE *********************

I did some reading up and found a routine to confirm at run-time the names and values of an enum.
I found that the values of our WCF enum were *not* what I expected....

This code:

Array enums = Enum.GetValues(typeof(JobLevelIdEnum));
            String sResult = null;
            for (int i = 0; i < enums.Length; i++)
                sResult += String.Format("Name: {0}, Value: {0:D}\r\n", enums.GetValue(i));

Open in new window

Produces this output in the String sResult:

Name: Item16, Value: 0
Name: Item10, Value: 1
Name: Item11, Value: 2
Name: Item12, Value: 3
Name: Item13, Value: 4
Name: Item14, Value: 5
Name: Item15, Value: 6

So the names *do not map* nicely onto the values.

I've been told to return strings in the range "10" to "16" in the XML message.
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zorba111


There are 2x workarounds: the one suggested by OP, and the one I used in the end. I decided to go with my approach as although it involved a bit more coding, it avoided having to edit a generated code file (whereby the edits are easily lost if the service gets regenerated, and also hard to document for same reason).