473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 5211
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
6
by: Catherine Jones | last post by:
Hi all, we need urgent help in a matter. We are trying to pass a COM object from the client to server and are facing some problems in the same. We've our client in C# as well as the Server...
7
by: tony collier | last post by:
if i have a string eg string day="monday"; and an enumerator enum days {monday=0,tuesday, wednesday}
6
by: | last post by:
I have MyEnumeratedSet { ... some values go here ... } I have a variable that is defined as MyEnumeratedSet MyVariable; I have a function that needs to be able to set a variable by Ref...
16
by: Simon | last post by:
Hi all, I think I've seen someone passing an emumeration in code before. Can anyone tell me if thats possible and why i would want to. Many thanks Kindest Regards
7
by: chukkykzn | last post by:
Hi I'd like to pass an entire enumeration to a method. Not sure how it's done. Eg: enum MyEnum { one = 1, two = 2 } class Test
3
by: IntraRELY | last post by:
I have the following function, Notice how I am passing the dateInterval as a string. What is the correct way to pass "DateInterval.Year" as a variable to a function? TIA, Steve Wofford...
1
by: martin | last post by:
This code compiles (this is how it should work): Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); This code does not compile (passing an int instead of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.