473,714 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get Enum type using Enum name?

Don
How do I get an Enum's type using only the Enum name?

e.g.

Dim enumType as System.Type
Dim enumName as String = "MyEnum"

enumType = ???(enumName)
I've tried using Type.GetType(en umName) to retrieve an Enum's type by name,
but I always get the value Nothing in return.

- Don
Nov 21 '05 #1
13 12382
not sure i follow, don...

all enums only have integer/numeric members. perhaps you mean something
else?
"Don" <un*****@oblivi on.com> wrote in message
news:EaD4e.9045 75$Xk.345025@pd 7tw3no...
| How do I get an Enum's type using only the Enum name?
|
| e.g.
|
| Dim enumType as System.Type
| Dim enumName as String = "MyEnum"
|
| enumType = ???(enumName)
|
|
| I've tried using Type.GetType(en umName) to retrieve an Enum's type by
name,
| but I always get the value Nothing in return.
|
| - Don
|
|
Nov 21 '05 #2
Don
?

I don't understand what the possible values of an Enum has to do with
getting a Type object for an Enum.
"steve" <a@b.com> wrote in message news:Mo******** ********@fe04.l ga...
not sure i follow, don...

all enums only have integer/numeric members. perhaps you mean something
else?
"Don" <un*****@oblivi on.com> wrote in message
news:EaD4e.9045 75$Xk.345025@pd 7tw3no...
| How do I get an Enum's type using only the Enum name?
|
| e.g.
|
| Dim enumType as System.Type
| Dim enumName as String = "MyEnum"
|
| enumType = ???(enumName)
|
|
| I've tried using Type.GetType(en umName) to retrieve an Enum's type by
name,
| but I always get the value Nothing in return.
|
| - Don
|
|

Nov 21 '05 #3
"Don" <un*****@oblivi on.com> schrieb:
How do I get an Enum's type using only the Enum name?

e.g.

Dim enumType as System.Type
Dim enumName as String = "MyEnum"

enumType = ???(enumName)
I've tried using Type.GetType(en umName) to retrieve an Enum's type by
name,
but I always get the value Nothing in return.

Make sure you fully qualify the type name correctly. You can determine a
type's full name by using 'GetType(<type name>).FullName '.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4
the quandry, of course, is that the type of object of an enum is...enum. the
possible values of the members of an enum were not what i commented on, but
the fact that the members themselves are all, and always, integer values.

hence, to me, you question is a bit vexing...an enum object is an enum type
and its members are integers.

could you be more specific is what it is you'd like to discover about an
enum?
"Don" <un*****@oblivi on.com> wrote in message
news:osD4e.9084 11$6l.111545@pd 7tw2no...
| ?
|
| I don't understand what the possible values of an Enum has to do with
| getting a Type object for an Enum.
|
|
| "steve" <a@b.com> wrote in message news:Mo******** ********@fe04.l ga...
| > not sure i follow, don...
| >
| > all enums only have integer/numeric members. perhaps you mean something
| > else?
| >
| >
| > "Don" <un*****@oblivi on.com> wrote in message
| > news:EaD4e.9045 75$Xk.345025@pd 7tw3no...
| > | How do I get an Enum's type using only the Enum name?
| > |
| > | e.g.
| > |
| > | Dim enumType as System.Type
| > | Dim enumName as String = "MyEnum"
| > |
| > | enumType = ???(enumName)
| > |
| > |
| > | I've tried using Type.GetType(en umName) to retrieve an Enum's type by
| > name,
| > | but I always get the value Nothing in return.
| > |
| > | - Don
| > |
| > |
| >
| >
|
|
Nov 21 '05 #5
lemme guess, you have an function/sub/property param that could be one of
many enum's and you're trying to decide, by name, which to use for
conversion/validation/action to preform?

is it that or something similar?
"Don" <un*****@oblivi on.com> wrote in message
news:osD4e.9084 11$6l.111545@pd 7tw2no...
| ?
|
| I don't understand what the possible values of an Enum has to do with
| getting a Type object for an Enum.
|
|
| "steve" <a@b.com> wrote in message news:Mo******** ********@fe04.l ga...
| > not sure i follow, don...
| >
| > all enums only have integer/numeric members. perhaps you mean something
| > else?
| >
| >
| > "Don" <un*****@oblivi on.com> wrote in message
| > news:EaD4e.9045 75$Xk.345025@pd 7tw3no...
| > | How do I get an Enum's type using only the Enum name?
| > |
| > | e.g.
| > |
| > | Dim enumType as System.Type
| > | Dim enumName as String = "MyEnum"
| > |
| > | enumType = ???(enumName)
| > |
| > |
| > | I've tried using Type.GetType(en umName) to retrieve an Enum's type by
| > name,
| > | but I always get the value Nothing in return.
| > |
| > | - Don
| > |
| > |
| >
| >
|
|
Nov 21 '05 #6
Don
When you create an Enum, you create a new Type. Any variable that is
declared as an Enum has that Enum as it's type. A variable is not of type
Integer when declared as an Enum you've created. The System.Type class even
has a property call IsEnum. I've written other code that retrieves all
nested types of a class, and Enums declared in the class are returned as
Types. I know you can get the Type of an Enum. I just can't seem to get it
to work when I'm just passing the name of the Enum to Type.GetType().

I am trying to determine all the members of an Enum, given only the Enum's
name. That is, I want to get the Enum's Type so I can use the
Enum.GetNames() or Enum.GetValues( ) method to retrieve a list of all of its
members. I don't know how else to put it.

"steve" <a@b.com> wrote in message news:hX******** *********@fe07. lga...
the quandry, of course, is that the type of object of an enum is...enum. the possible values of the members of an enum were not what i commented on, but the fact that the members themselves are all, and always, integer values.

hence, to me, you question is a bit vexing...an enum object is an enum type and its members are integers.

could you be more specific is what it is you'd like to discover about an
enum?
"Don" <un*****@oblivi on.com> wrote in message
news:osD4e.9084 11$6l.111545@pd 7tw2no...
| ?
|
| I don't understand what the possible values of an Enum has to do with
| getting a Type object for an Enum.
|
|
| "steve" <a@b.com> wrote in message news:Mo******** ********@fe04.l ga...
| > not sure i follow, don...
| >
| > all enums only have integer/numeric members. perhaps you mean something | > else?
| >
| >
| > "Don" <un*****@oblivi on.com> wrote in message
| > news:EaD4e.9045 75$Xk.345025@pd 7tw3no...
| > | How do I get an Enum's type using only the Enum name?
| > |
| > | e.g.
| > |
| > | Dim enumType as System.Type
| > | Dim enumName as String = "MyEnum"
| > |
| > | enumType = ???(enumName)
| > |
| > |
| > | I've tried using Type.GetType(en umName) to retrieve an Enum's type by | > name,
| > | but I always get the value Nothing in return.
| > |
| > | - Don
| > |
| > |
| >
| >
|
|

Nov 21 '05 #7
Don
Hmmm... What you suggest works, but only if the Enum is declared in the same
project. If I try to do the same thing with an Enum declared in a
referenced dll, then the Type.GetType() method returns Nothing.

This is the code I am executing:

Public Enum abc
val1 = 1
val2 = 2
End Enum

Dim s As String
s = GetType(abc).Fu llName

Dim t As Type
t = Type.GetType(s) ' <--- Works!

This works fine. However, if, say, the "abc" Enum existed in a dll that I
was referencing, the code does not work:

' (assuming "abc" is a publicly declared Enum in a public module called
' "Enumeratio ns" in the referenced class library "OtherDLL")
Dim s As String
s = GetType(OtherDL L.Enumerations. abc).FullName

Dim t As Type
t = Type.GetType(s) ' <--- Fails!
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:O6******** ******@tk2msftn gp13.phx.gbl...
"Don" <un*****@oblivi on.com> schrieb:
How do I get an Enum's type using only the Enum name?

e.g.

Dim enumType as System.Type
Dim enumName as String = "MyEnum"

enumType = ???(enumName)
I've tried using Type.GetType(en umName) to retrieve an Enum's type by
name,
but I always get the value Nothing in return.

Make sure you fully qualify the type name correctly. You can determine a
type's full name by using 'GetType(<type name>).FullName '.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8
Don
I just discovered that the following code works:

' (assuming "abc" is a publicly declared Enum in a public module called
' "Enumeratio ns" in the referenced class library "OtherDLL")
Dim s As String
s = GetType(OtherDL L.Enumerations. abc).AssemblyQu alifiedName '<--- NOT
FullName

Dim t As Type
t = Type.GetType(s) ' <--- Works!

Thanks for pointing me into the right direction.

- Don
"Don" <un*****@oblivi on.com> wrote in message
news:ZrR4e.9147 68$8l.831814@pd 7tw1no...
Hmmm... What you suggest works, but only if the Enum is declared in the same project. If I try to do the same thing with an Enum declared in a
referenced dll, then the Type.GetType() method returns Nothing.

This is the code I am executing:

Public Enum abc
val1 = 1
val2 = 2
End Enum

Dim s As String
s = GetType(abc).Fu llName

Dim t As Type
t = Type.GetType(s) ' <--- Works!

This works fine. However, if, say, the "abc" Enum existed in a dll that I
was referencing, the code does not work:

' (assuming "abc" is a publicly declared Enum in a public module called ' "Enumeratio ns" in the referenced class library "OtherDLL")
Dim s As String
s = GetType(OtherDL L.Enumerations. abc).FullName

Dim t As Type
t = Type.GetType(s) ' <--- Fails!
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:O6******** ******@tk2msftn gp13.phx.gbl...
"Don" <un*****@oblivi on.com> schrieb:
How do I get an Enum's type using only the Enum name?

e.g.

Dim enumType as System.Type
Dim enumName as String = "MyEnum"

enumType = ???(enumName)
I've tried using Type.GetType(en umName) to retrieve an Enum's type by
name,
but I always get the value Nothing in return.

Make sure you fully qualify the type name correctly. You can determine a type's full name by using 'GetType(<type name>).FullName '.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


Nov 21 '05 #9
Don
Duh. I forgot that I will only have the name of the Enum as a string to
start with. It doesn't look like I'll be able to do this because the
AssembylQualifi edName -- which is what I need to start off with -- contains
version information which may change, so it cannot be hardcoded into my
project. Argh!
"Don" <un*****@oblivi on.com> wrote in message
news:IAR4e.9157 74$6l.884013@pd 7tw2no...
I just discovered that the following code works:

' (assuming "abc" is a publicly declared Enum in a public module called ' "Enumeratio ns" in the referenced class library "OtherDLL")
Dim s As String
s = GetType(OtherDL L.Enumerations. abc).AssemblyQu alifiedName '<--- NOT FullName

Dim t As Type
t = Type.GetType(s) ' <--- Works!

Thanks for pointing me into the right direction.

- Don
"Don" <un*****@oblivi on.com> wrote in message
news:ZrR4e.9147 68$8l.831814@pd 7tw1no...
Hmmm... What you suggest works, but only if the Enum is declared in the same
project. If I try to do the same thing with an Enum declared in a
referenced dll, then the Type.GetType() method returns Nothing.

This is the code I am executing:

Public Enum abc
val1 = 1
val2 = 2
End Enum

Dim s As String
s = GetType(abc).Fu llName

Dim t As Type
t = Type.GetType(s) ' <--- Works!

This works fine. However, if, say, the "abc" Enum existed in a dll that I
was referencing, the code does not work:

' (assuming "abc" is a publicly declared Enum in a public module

called
' "Enumeratio ns" in the referenced class library "OtherDLL")
Dim s As String
s = GetType(OtherDL L.Enumerations. abc).FullName

Dim t As Type
t = Type.GetType(s) ' <--- Fails!
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:O6******** ******@tk2msftn gp13.phx.gbl...
"Don" <un*****@oblivi on.com> schrieb:
> How do I get an Enum's type using only the Enum name?
>
> e.g.
>
> Dim enumType as System.Type
> Dim enumName as String = "MyEnum"
>
> enumType = ???(enumName)
>
>
> I've tried using Type.GetType(en umName) to retrieve an Enum's type by > name,
> but I always get the value Nothing in return.
Make sure you fully qualify the type name correctly. You can

determine a type's full name by using 'GetType(<type name>).FullName '.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>



Nov 21 '05 #10

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

Similar topics

20
4846
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 EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
6
796
by: James Brown | last post by:
Hi, I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING, /*lots more here*/ }; What I am trying to do is _also_ represent ASCII values 0-127 as TOKENs (this is why I started the TOKEN enum off at '1000' so I had plenty of space at the
4
3051
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum { DOUBLE_LIST, INT_LIST } DATA_TYPE; typedef struct { DATA_TYPE type;
21
4596
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
9
39704
by: Lawrence Oluyede | last post by:
I have a list of strings and i'd like to build up an enum from them... is there a way to do that? Thanks in advance. -- Lawrence "Rhymes" Oluyede http://loluyede.blogspot.com
5
2869
by: Barry | last post by:
Hello, In VS2003, I tried using an enum and setting it into a field in a datarow. It seems as if the datatype of the field in the row determined what went into the field. If the datatype was string, then the name of the enum item went into the field, but if the datatype was integer, then the integer value of the enum was stored. Is this expected behaviour? I couldn't find anything while searching for answers.
1
5238
by: Powers | last post by:
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
13
18143
by: toton | last post by:
Hi, I have some enum (enumeration ) defined in some namespace, not inside class. How to use the enum constant's in some other namespace without using the whole namespace. To say in little detail, the enum is declared as, namespace test{ enum MyEnum{ VALUE1,VALUE2 };
34
11193
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
0
8701
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
9306
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
9168
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9068
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7942
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...
0
4715
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3155
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
2510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2103
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.