WCF enums not functioning like "vanilla" enums

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

    enum EmpType
    {
        Grunt,
        Contractor,
        Manager,
        VicePresident
    }

// 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")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://schemas.monster.com/Monster")]
    public enum JobLevelIdEnum {
        
        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("16")]
        Item16,
        
        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("10")]
        Item10,
        
        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("11")]
        Item11,
        
        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("12")]
        Item12,
        
        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("13")]
        Item13,
        
        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("14")]
        Item14,
        
        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("15")]
        Item15,
    }

// 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;
            else
                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.
zorba111Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi zorba111;

I am not sure what the XmlEnumAttribute will be doing but normally attributes are there as metadata where other code can check on its existence and if found can use it, in this case used to serialize and deserialize the XML. The following should correct the issue you are having. I made two changes to the first two enums.

 public enum JobLevelIdEnum {
        
        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("16")]
        Item16 = 16,
        
        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("10")]
        Item10 = 10,
        
        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("11")]
        Item11,
        
        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("12")]
        Item12,
        
        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("13")]
        Item13,
        
        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("14")]
        Item14,
        
        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("15")]
        Item15,
    }

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zorba111Author Commented:
Hi Fernando, Thanks - yes I had thought about doing this but I fought shy of changing any generated code.

In the end I decided to use a switch/case statement to translate the values from the combo box into their enum equivalents.

            switch (vac.MONSTERlevel)
            {
                case "10":
                    level = JobLevelIdEnum.Item10;
                    break;
                case "11":
                    level = JobLevelIdEnum.Item11;
                    break;
                case "12":
                    level = JobLevelIdEnum.Item12;
                    break;
                case "13":
                    level = JobLevelIdEnum.Item13;
                    break;
                case "14":
                    level = JobLevelIdEnum.Item14;
                    break;
                case "15":
                    level = JobLevelIdEnum.Item15;
                    break;
                case "16":                          //i.e. "none of these"
                default:
                    level = JobLevelIdEnum.Item16;
                    break;
            }

Open in new window


I think its a deficiency of the WSDL or the schema. I noticed other enums in reference.cs where the names match up to the actual values. Whoever wrote the WSDL just forgot to do it for the JobLevelIdEnum.

UPDATE: This enum JobLevelIdEnum is defined in a schema file Enumerations.xsd that gets pulled into the .wsdl file at 2nd level. The string numbers might as well be word descriptors - they bear no relation to the enum values, and it's wrong to think of the enums as mapping onto the string numbers..... IMHO....

Of course it might have been easier, if we're going to give .xsd enums names that are numbers, at least make the enum values match up... as it will make input validation and parsing easier...but heigh-ho...
0
zorba111Author Commented:
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).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WCF

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.