Home All Groups Group Topic Archive Search About

Passing System.Enum as a parameter type... Help!

Author
29 Aug 2006 9:55 AM
Powers
I currently have a large number of enums implemented into my Web
Service.  I am reworking the XML serialization of these enums.

At present, each enum has hardcoded values, for example:

            xAttrs = New XmlAttributes
            xEnum = New XmlEnumAttribute
            xEnum.Name = "0"
            xAttrs.XmlEnum = xEnum

objXmlAttributeOverrides.Add(GetType(Enum_Employment_Status),
"Unspecified", xAttrs)

            xAttrs = New XmlAttributes
            xEnum = New XmlEnumAttribute
            xEnum.Name = "1"
            xAttrs.XmlEnum = xEnum

objXmlAttributeOverrides.Add(GetType(Enum_Employment_Status),
"FullTime", xAttrs)

            xAttrs = New XmlAttributes
            xEnum = New XmlEnumAttribute
            xEnum.Name = "2"
            xAttrs.XmlEnum = xEnum

objXmlAttributeOverrides.Add(GetType(Enum_Employment_Status),
"SelfEmployed", xAttrs)

            xAttrs = New XmlAttributes
            xEnum = New XmlEnumAttribute
            xEnum.Name = "4"
            xAttrs.XmlEnum = xEnum

objXmlAttributeOverrides.Add(GetType(Enum_Employment_Status),
"PieceWorker", xAttrs)

            xAttrs = New XmlAttributes
            xEnum = New XmlEnumAttribute
            xEnum.Name = "8"
            xAttrs.XmlEnum = xEnum

objXmlAttributeOverrides.Add(GetType(Enum_Employment_Status),
"ContractWorker", xAttrs)


....this streams for some 2000+ lines, hence why the rework.

I've removed the need to hardcode values, and I've created a method to
deal with the serialization, allowing me to simply make a method call
per enum, and removing the need to hardcode the enum values.  The
method accepts the XMLAttributeOverrides object, and the Enum type.

The code is as below:

        Private Sub SerializeEnum(ByVal pEnum As System.Enum, ByVal
pxOver As XmlAttributeOverrides)
            'Powers - unable to do this, can't pass in the enum type...

            Try
                Dim strEnumContents As String()
                Dim strEnum As String
                Dim xAttrs As XmlAttributes
                Dim xEnum As XmlEnumAttribute

                strEnumContents = [Enum].GetValues(GetType(pEnum))

                For Each strEnum In strEnumContents
                    xAttrs = New XmlAttributes
                    xEnum = New XmlEnumAttribute
                    xEnum.Name = [Enum].Format(GetType(pEnum), strEnum,
"d")
                    xAttrs.XmlEnum = xEnum
                    pxOver.Add(GetType(pEnum),
[Enum].GetName(GetType(pEnum), strEnum), xAttrs)
                Next

            Catch ex As Exception
                'LogException ex

            End Try

        End Sub


The problem now arises at each GetType(pEnum) - the compiler complains
that "Type 'pEnum' is not defined".

Is there a way I can do this, or am I going to have to hardcode the
loop for each individual enum??  I've tried using System.Type instead
of System.Enum, but no luck...

Any help would be great...

TIA,
Powers

Author
29 Aug 2006 7:54 PM
Andrew Backer
I think you can do something like this, if you need generic access to
an enum's types.  At least it is something that works in my case.
You need to get at what you need via the static methods, which you
can't call on an instance here (?).  So, use

=> System.Enum.AnyStaticMethod( System.Type of your enum )

Public Enum DBA
    SqlSeverLimbs = 2
    MyCrashQl = 3
    PostgresCantPronounce = 4
End Enum

Public Function EnumNames(ByVal enumType System.Type ) as String()
    Dim names as String() = System.Enum.GetNames(enumType)
End Function

Dim servers as String() = EnumNames( GetType(DBA) )

Hope this helps.

- Andrew

Powers wrote:
Show quoteHide quote
> I currently have a large number of enums implemented into my Web
> Service.  I am reworking the XML serialization of these enums.
>
> At present, each enum has hardcoded values, for example:
>
>             xAttrs = New XmlAttributes
>             xEnum = New XmlEnumAttribute
....
>         Private Sub SerializeEnum(ByVal pEnum As System.Enum, ByVal
> pxOver As XmlAttributeOverrides)
>             'Powers - unable to do this, can't pass in the enum type...
....
>                 strEnumContents = [Enum].GetValues(GetType(pEnum))
>
....
> The problem now arises at each GetType(pEnum) - the compiler complains
> that "Type 'pEnum' is not defined".
>
> Is there a way I can do this, or am I going to have to hardcode the
> loop for each individual enum??  I've tried using System.Type instead
> of System.Enum, but no luck...