473,721 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get enumeration type

Don
I need to get the type of an enumeration from an instance of a class.

e.g.

Public Class MyClass
Public Enum MyEnum
value1 = 1
End Enum
End Class

....

Dim obj as New MyClass
Dim enumType as Type

enumType = the type of obj.MyEnum
Is there any way to do this?

- Don
Nov 21 '05 #1
7 9017

"Don" <un*****@oblivi on.com> wrote in message
news:TBWad.8661 0$a41.31746@pd7 tw2no...
I need to get the type of an enumeration from an instance of a class.

e.g.

Public Class MyClass
Public Enum MyEnum
value1 = 1
End Enum
End Class

...

Dim obj as New MyClass
Dim enumType as Type

enumType = the type of obj.MyEnum
Is there any way to do this?


Dim objEnum As MyClass.MyEnum
Dim enumType As Type = objEnum.GetType ()
hope that helps..
Imran.
Nov 21 '05 #2
Don
Maybe I should have been more clear: I have multiple classes with an
enumeration of the same name in each, but each enumeration is slightly
different. I don't know what type the object will be at runtime, so I can't
do MyClass.MyEnum. I need to be able to get the type of the object, then
get the type of the enum in that class.
"Imran Koradia" <no****@microso ft.com> wrote in message
news:uK******** ******@tk2msftn gp13.phx.gbl...

"Don" <un*****@oblivi on.com> wrote in message
news:TBWad.8661 0$a41.31746@pd7 tw2no...
I need to get the type of an enumeration from an instance of a class.

e.g.

Public Class MyClass
Public Enum MyEnum
value1 = 1
End Enum
End Class

...

Dim obj as New MyClass
Dim enumType as Type

enumType = the type of obj.MyEnum
Is there any way to do this?


Dim objEnum As MyClass.MyEnum
Dim enumType As Type = objEnum.GetType ()
hope that helps..
Imran.

Nov 21 '05 #3
"Don" <un*****@oblivi on.com> schrieb:
Maybe I should have been more clear: I have multiple
classes with an enumeration of the same name in each,
but each enumeration is slightly different. I don't know
what type the object will be at runtime, so I can't
do MyClass.MyEnum. I need to be able to get the
type of the object, then get the type of the enum in that class.


'GetType' on the object holding the enum value will do what you want, as far
as I understand. But that's what Imran already suggested.
Dim enumType As Type = objEnum.GetType ()


--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #4
Dim obj As New MyClass
Dim enumType = obj.GetType().G etNestedType("M yEnum")
Messagebox.Show (enumType.Name)

Note that the search is case-sensitive; So "myEnum" instead of "MyEnum" will
return nothing.
hope that helps..
Imran.

"Don" <un*****@oblivi on.com> wrote in message
news:ybXad.8718 1$a41.9859@pd7t w2no...
Maybe I should have been more clear: I have multiple classes with an
enumeration of the same name in each, but each enumeration is slightly
different. I don't know what type the object will be at runtime, so I can't do MyClass.MyEnum. I need to be able to get the type of the object, then
get the type of the enum in that class.
"Imran Koradia" <no****@microso ft.com> wrote in message
news:uK******** ******@tk2msftn gp13.phx.gbl...

"Don" <un*****@oblivi on.com> wrote in message
news:TBWad.8661 0$a41.31746@pd7 tw2no...
I need to get the type of an enumeration from an instance of a class.

e.g.

Public Class MyClass
Public Enum MyEnum
value1 = 1
End Enum
End Class

...

Dim obj as New MyClass
Dim enumType as Type

enumType = the type of obj.MyEnum
Is there any way to do this?


Dim objEnum As MyClass.MyEnum
Dim enumType As Type = objEnum.GetType ()
hope that helps..
Imran.


Nov 21 '05 #5
Don
That's exactly what I need. It only seems to work if the enumeration is
public, though.

Also, what is returned doesn't appear to be accepted by the
System.Enum.Get Name(), method. It's like it doesn't recognize it as an
enumeration if I get the enumeration type in this manner. Any thoughts?

- Don
"Imran Koradia" <no****@microso ft.com> wrote in message
news:%2******** **********@TK2M SFTNGP12.phx.gb l...
Dim obj As New MyClass
Dim enumType = obj.GetType().G etNestedType("M yEnum")
Messagebox.Show (enumType.Name)

Note that the search is case-sensitive; So "myEnum" instead of "MyEnum" will return nothing.

Nov 21 '05 #6

"Don" <un*****@oblivi on.com> wrote in message
news:vdYad.8867 2$a41.40172@pd7 tw2no...
That's exactly what I need. It only seems to work if the enumeration is
public, though.
To get the both public and private enums, use the other overloaded version
of GetNestedType:

Dim obj As New MyClass
Dim enumType = obj.GetType().G etNestedType("M yEnum", _
Reflection.Bind ingFlags.Public Or Reflection.Bind ingFlags.NonPub lic)
Messagebox.Show (enumType.Name)

Also, what is returned doesn't appear to be accepted by the
System.Enum.Get Name(), method. It's like it doesn't recognize it as an
enumeration if I get the enumeration type in this manner. Any thoughts?


This worked for me:
MessageBox.Show (System.Enum.Ge tName(enumType, 1))

Is that not what you are looking for?

hope that helps..
Imran.
Nov 21 '05 #7
Don
"Imran Koradia" <no****@microso ft.com> wrote in message

news:<#n******* *******@tk2msft ngp13.phx.gbl>. ..
"Don" <un*****@oblivi on.com> wrote in message news:vdYad.8867 2$a41.40172@pd7 tw2no...
That's exactly what I need. It only seems to work if the enumeration is public, though. To get the both public and private enums, use the other overloaded version of GetNestedType: Dim obj As New MyClass Dim enumType = obj.GetType().G etNestedType("M yEnum", _ Reflection.Bind ingFlags.Public Or
Reflection.Bind ingFlags.NonPub lic)
Messagebox.Show (enumType.Name) Also, what is returned doesn't appear to be accepted by the System.Enum.Get Name(), method. It's like it doesn't recognize it as an enumeration if I get the enumeration type in this manner. Any thoughts?

This worked for me: MessageBox.Show (System.Enum.Ge tName(enumType, 1)) Is that not what you are looking for?


Yes, this is exactly what I'm looking for and I'm doing pretty much same

thing you are (obj is passed as a parameter and not instantiated in the

function, as in your example, though), but the call to Enum.GetName raises

the following error: "Type provided must be an Enum." When I use

obj.GetType().G etNestedType() to return an Enum's Type and store it into a

"type" variable, it doesn't seem to remember that it really is an enum.

Thanks for your help. This is exactly what I need. I just wish it would

work properly. :(

- Don
Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
12631
by: Justin Wright | last post by:
I know that I can set up an enumeration as follows ( just typed in quick so may have syntax errors ): <xsd:simpleType name="colors"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="red"/> <xsd:enumeration value="yellow"/> <xsd:enumeration value="blue"/> </xsd:simpleType>
0
1634
by: Fender Mussel | last post by:
Hi all, Is it possible to check the content of a mixed type? I would like to define an enumeration for it. The following example works syntactically, but XMLSpy does no checks on the content at all: <xsd:simpleType name="my_enum_type"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="enum1"/> <xsd:enumeration value="enum2"/>
8
6510
by: aevans1108 | last post by:
Greetings I can't seem to inherit enumerated values from a globally defined type in my XML schema. XmlSchema.Compile() doesn't like it. Here's the schema. <?xml version="1.0" encoding="UTF-8" ?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
1
3666
by: Sergey Poberezovskiy | last post by:
Hi, I have a simple enumeration in my schema: <xs:element name="el_1"> <xs:simpleType> <xs:restiction base="xs:string"> <xs:enumeration value="value and space 1"/> <xs:enumeration value="value2 with spaces"/> ...
3
10628
by: Sampson | last post by:
I have a question about enumeration and how to populate them during runtime. I am using vb.net but will happily take any advice in c# as well. Here is an example to help illustrate what I am after. Create a class named “clsMyItems” and in that class place an enum. Public Enum Items Item1 = 0 Item2 = 1 Item3 = 2
27
2670
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to provide a "one obvious way" to do enumerations in Python. > >>> from enum import Enum > >>> day = Enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')
3
2708
by: eXt | last post by:
For a realtime-application (half an application at least, kind of a framework) I am working with I need a event managing system and naturally it must be fast. I have defined a set of available event types in an enumeration like this: enum Events { EVENT_FOO, EVENT_BAR, EVENT_BAZ, EVENT_CUSTOM1,
3
4791
by: muler | last post by:
hi all, After reading this excerpt from "The C# Programming Language", (By Anders) I tried to check it out. Unfortunately, I'm getting compile errors. Can anyone illustrate this with an example: ---------------------------------------------------- Section 7.7.4 - enumeration addition
0
1858
by: puvit82 | last post by:
My problem is as follows, any advice / suggestion would be greatly appreciated: Lets suppose that I have defined a simpleType "addressType" with 3 enumeration values (Home, Office, Vacation) that restrict data entry. I want to use this simpleType in 2 different unrelated complexType definitions, lets call them "personalAddress" and "companyAddress" How will I be able to use "addressType" inside "personalAddress" such that the 3...
0
8734
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9372
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9071
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8013
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6675
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4758
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3197
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2581
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2135
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.