473,386 Members | 1,810 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,386 software developers and data experts.

Programmaticallygetting type of an enum

Scenario:
- I'm dumping an int value of myEnum into a table.
- I am getting that int value out of the table.
- I need to set myEnum equal to that int value

Easy enough:
private enum animals {cat=0, dog=1}
private animals myAnimal = (animals)1;

OK- so that works, and myAnimal is set to dog....

Problem:
I'm using reflection to go through a list of properties and am setting them
to their respective values from a table. My problem is that I need to
<<programmatically>> get type myAnimal.

private animals myAnimal = (System.Enum)1; //doesn't work
private animals myAnimal = 1; //doesn't work
private animals myAnimal = (programatically get type myAnimal)1; //this is
what I need

Anyone have any suggestions???

Thanks,
Tom


Nov 15 '05 #1
2 1698
Tom,
Have you tried the Enum.ToObject function?

private animals myAnimal = Enum.ToObject(typeof(animals), 1);

Of course if you already have a variable of type System.Type you can use
that on ToObject instead of typeof. (if you are using reflection you should
have a variable of type System.Type!

Hope this helps
Jay

"Tom Hundley" <thomas_hundley@_nospam_hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Scenario:
- I'm dumping an int value of myEnum into a table.
- I am getting that int value out of the table.
- I need to set myEnum equal to that int value

Easy enough:
private enum animals {cat=0, dog=1}
private animals myAnimal = (animals)1;

OK- so that works, and myAnimal is set to dog....

Problem:
I'm using reflection to go through a list of properties and am setting them to their respective values from a table. My problem is that I need to
<<programmatically>> get type myAnimal.

private animals myAnimal = (System.Enum)1; //doesn't work
private animals myAnimal = 1; //doesn't work
private animals myAnimal = (programatically get type myAnimal)1; //this is what I need

Anyone have any suggestions???

Thanks,
Tom

Nov 15 '05 #2
Yup, worked great. Thanks!

val = Enum.ToObject(pi.PropertyType,
(int)Source.GetType().GetProperty(_propertyPrefix +
pi.Name).GetValue(Source, null));
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Tom,
Have you tried the Enum.ToObject function?

private animals myAnimal = Enum.ToObject(typeof(animals), 1);

Of course if you already have a variable of type System.Type you can use
that on ToObject instead of typeof. (if you are using reflection you should have a variable of type System.Type!

Hope this helps
Jay

"Tom Hundley" <thomas_hundley@_nospam_hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Scenario:
- I'm dumping an int value of myEnum into a table.
- I am getting that int value out of the table.
- I need to set myEnum equal to that int value

Easy enough:
private enum animals {cat=0, dog=1}
private animals myAnimal = (animals)1;

OK- so that works, and myAnimal is set to dog....

Problem:
I'm using reflection to go through a list of properties and am setting

them
to their respective values from a table. My problem is that I need to
<<programmatically>> get type myAnimal.

private animals myAnimal = (System.Enum)1; //doesn't work
private animals myAnimal = 1; //doesn't work
private animals myAnimal = (programatically get type myAnimal)1; //this

is
what I need

Anyone have any suggestions???

Thanks,
Tom


Nov 15 '05 #3

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

Similar topics

21
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 {
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
13
by: Don | last post by:
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)
2
by: Alex Feldman | last post by:
Which of the following is better? Defining an enum type inside a class as a nested type, or in the the namespace? An example of nested type enumerated type would be: public Class Product...
14
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...
10
by: Randy | last post by:
Hi, Can anyone point me to a complete, compilable example of Besser's ENUM++ mechanism? I downloaded it from CUJ and gave it a try but got errors just trying to compile the header enum.h. ...
1
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
10
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize...
34
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.