469,646 Members | 1,558 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

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


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_Employme nt_Status),
"Unspecified", xAttrs)

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

objXmlAttributeOverrides.Add(GetType(Enum_Employme nt_Status),
"FullTime", xAttrs)

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

objXmlAttributeOverrides.Add(GetType(Enum_Employme nt_Status),
"SelfEmployed", xAttrs)

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

objXmlAttributeOverrides.Add(GetType(Enum_Employme nt_Status),
"PieceWorker", xAttrs)

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

objXmlAttributeOverrides.Add(GetType(Enum_Employme nt_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

Aug 29 '06 #1
1 4483
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:
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...
Aug 29 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by kazack | last post: by
20 posts views Thread by Glenn Venzke | last post: by
6 posts views Thread by Catherine Jones | last post: by
7 posts views Thread by tony collier | last post: by
16 posts views Thread by Simon | last post: by
7 posts views Thread by chukkykzn | last post: by
3 posts views Thread by IntraRELY | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.