views:

991

answers:

3

We're using Flex 3 to consume a .NET Web Service. The web service uses an enum and we use Flex Builder to automatically generate the web service objects. Here is the .NET web service enum definition:

 /// <summary>
   /// User type
   /// </summary>
   public enum UserTypeEnum
   {
       Admin = 1,
       User = 3,
       Manager = 4
   }

When Flex generates the web service objects, it treats them as strings instead of integer values.

[Inspectable(category="Generated values", enumeration="Admin, User, Manager", type="String")]
public var _UserTypeEnum:String;public function toString():String
{
return _UserTypeEnum.toString();
}

When we get data from the web service, any property that uses this enumeration is Null instead of 'admin' or '1'. Is there any way to make this work?

A: 

There are some bugs listed at Adobe re: Flex 3 and enum's in WSDL .. Are you sure you have the latest update ?

Scott Evernden
+1  A: 

Be aware that there are issues with enumerations and web services.

See this question.

  • Should be using [Serializable,Flags] in web service.
  • Flags should be multiples of two
  • The enumeration definition should start from 1 rather than 0.
nzpcmad
Minor pedantry I know, but *powers* of two surely, not simple multiples... :)
inferis
`[Serializable]` is ignored by XML Serialization.
John Saunders
A: 

In the languages derived from C (C#, C++, Java), an enum is effectively a set of named integral values.

There is no corresponding concept in XML Schema, therefore there is no such concept in web services.

Before you or anyone else mentions the <xs:enumeration/> facet, that's not what it's for. <xs:enumeration/> provides a list (an enumeration) of the possible lexical values for a type. There is no way to associate numbers with those lexical values. The following enum:

public enum Enumeration1
{
    E1a = 1,
    E1b = 2,
    E1c = 4
}

becomes the following XML Schema:

<s:simpleType name="Enumeration1">
    <s:restriction base="s:string">
        <s:enumeration value="E1a"/>
        <s:enumeration value="E1b"/>
        <s:enumeration value="E1c"/>
    </s:restriction>
</s:simpleType>

Note the absence of 1, 2, or 4.

All this says is that an element or attribute of this type will be a string, and may have one of the values "E1a", "E1b", or "E1c".

If a client proxy is built from a WSDL with such schema, and if the proxy code generator is smart enough, it may decide that this was originally a programming language enum. In this case, it would create the type as

public enum Enumeration1
{
    E1a,
    E1b,
    E1c
}

Since it has no access to the integers.

John Saunders