finding an Enum using the string name of it

  • Thread starter YankeeImperialistDog
  • Start date
Y

YankeeImperialistDog

I have a simple function that returns a collection populated with an enum. It
is a bindable data object for reopdowns using an objectdatasource.I have
another version that takes a type for a param (type of enum) but i can not
assign a type enum as a paramater using an object datasource. Is there a
simple way to find the enum using a string?



[DataObjectMethod(DataObjectMethodType.Select, true)]
public static BasicListCollection eNumProductTypeDDL()
{
string[] names = Enum.GetNames(typeof(ProductType));
Array values = Enum.GetValues(typeof(ProductType));
var blc = new BasicListCollection();
for (int i = 0; i < names.Length; i++)
{
var bli = new BasicList();
bli.IID = Convert.ToInt32(values.GetValue(i));
bli.SzDesc = names;
blc.Add(bli);
}
return blc;
}
 
F

Family Tree Mike

YankeeImperialistDog said:
I have a simple function that returns a collection populated with an enum.
It
is a bindable data object for reopdowns using an objectdatasource.I have
another version that takes a type for a param (type of enum) but i can not
assign a type enum as a paramater using an object datasource. Is there a
simple way to find the enum using a string?



[DataObjectMethod(DataObjectMethodType.Select, true)]
public static BasicListCollection eNumProductTypeDDL()
{
string[] names = Enum.GetNames(typeof(ProductType));
Array values = Enum.GetValues(typeof(ProductType));
var blc = new BasicListCollection();
for (int i = 0; i < names.Length; i++)
{
var bli = new BasicList();
bli.IID = Convert.ToInt32(values.GetValue(i));
bli.SzDesc = names;
blc.Add(bli);
}
return blc;
}



Have a look at Enum.Parse:

enum RGB { RED, GREEN, BLUE }

static void Main(string[] args)
{
RGB rgb2 = (RGB) Enum.Parse(typeof (RGB), "GREEN");
}
 
Y

YankeeImperialistDog

Thanks for replying,

My issue is i'll be passing in a string that represents the enum not the
enum itself. What i'd like to do is get to the enum using a string.
my solution so far is:
[DataObjectMethod(DataObjectMethodType.Select, true)]
public static BasicListCollection eNumProductTypeDDL(string enumName)
{
Type typ = null;
switch (enumName.ToLower())
{
case "producttarget":
typ = typeof(ProductTarget);
break;
case "producttype":
typ = typeof (ProductType);
break;
case "billinginterval":
typ = typeof (BillingInterval);
break;
case "billingcycle":
typ = typeof(BillingCycle);
break;
case "billingtype":
typ = typeof(BillingType);
break;
case "trialperiod":
typ = typeof(TrialPeriod);
break;
default:
typ = typeof (PlaceHolder);
break;
}

string[] names = Enum.GetNames(typ);
Array values = Enum.GetValues(typ);
var blc = new BasicListCollection();
for (int i = 0; i < names.Length; i++)
{
var bli = new BasicList();
bli.IID = Convert.ToInt32(values.GetValue(i));
bli.SzDesc = names;
blc.Add(bli);
}
return blc;
}

but this is still hard coding a test for the enum. I'd like to find a way to
make it generic so to speak where i can maybe use reflection to get the emun?
not sure if this is the way or not.

I appreciate you advice and any additional light you can shed on this.

thanks
--
Share The Knowledge. I need all the help I can get and so do you!


Family Tree Mike said:
YankeeImperialistDog said:
I have a simple function that returns a collection populated with an enum.
It
is a bindable data object for reopdowns using an objectdatasource.I have
another version that takes a type for a param (type of enum) but i can not
assign a type enum as a paramater using an object datasource. Is there a
simple way to find the enum using a string?



[DataObjectMethod(DataObjectMethodType.Select, true)]
public static BasicListCollection eNumProductTypeDDL()
{
string[] names = Enum.GetNames(typeof(ProductType));
Array values = Enum.GetValues(typeof(ProductType));
var blc = new BasicListCollection();
for (int i = 0; i < names.Length; i++)
{
var bli = new BasicList();
bli.IID = Convert.ToInt32(values.GetValue(i));
bli.SzDesc = names;
blc.Add(bli);
}
return blc;
}



Have a look at Enum.Parse:

enum RGB { RED, GREEN, BLUE }

static void Main(string[] args)
{
RGB rgb2 = (RGB) Enum.Parse(typeof (RGB), "GREEN");
}
 
F

Family Tree Mike

YankeeImperialistDog said:
Thanks for replying,

My issue is i'll be passing in a string that represents the enum not the
enum itself. What i'd like to do is get to the enum using a string.
my solution so far is:
[DataObjectMethod(DataObjectMethodType.Select, true)]
public static BasicListCollection eNumProductTypeDDL(string
enumName)
{
Type typ = null;
switch (enumName.ToLower())
{
case "producttarget":
typ = typeof(ProductTarget);
break;
case "producttype":
typ = typeof (ProductType);
break;
case "billinginterval":
typ = typeof (BillingInterval);
break;
case "billingcycle":
typ = typeof(BillingCycle);
break;
case "billingtype":
typ = typeof(BillingType);
break;
case "trialperiod":
typ = typeof(TrialPeriod);
break;
default:
typ = typeof (PlaceHolder);
break;
}

string[] names = Enum.GetNames(typ);
Array values = Enum.GetValues(typ);
var blc = new BasicListCollection();
for (int i = 0; i < names.Length; i++)
{
var bli = new BasicList();
bli.IID = Convert.ToInt32(values.GetValue(i));
bli.SzDesc = names;
blc.Add(bli);
}
return blc;
}

but this is still hard coding a test for the enum. I'd like to find a way
to
make it generic so to speak where i can maybe use reflection to get the
emun?
not sure if this is the way or not.

I appreciate you advice and any additional light you can shed on this.

thanks


Oh, now I see. Try:

Type typ = Type.GetType(enumName, true, false);

You need to pass the fully qualified name for the enume however, which is
probably something like "MyProject.BillingType".

http://msdn.microsoft.com/en-us/library/a87che9d.aspx
 
Y

YankeeImperialistDog

it worked with a little modification. Thank you!
i had to load the assembly because it is in a different layer (different dll)

The one that worked
string str = "MyNamespace.EBS.BusinessEntities.AlertTypes";
Assembly assembly = null;
assembly = Assembly.Load("MyNamespace.EBS.BusinessEntities");
Type typ = assembly.GetType(str);
string[] names = Enum.GetNames(typ);
Array values = Enum.GetValues(typ);
var blc = new BasicListCollection();
for (int i = 0; i < names.Length; i++)
{
var bli = new BasicList();
bli.IID = Convert.ToInt32(values.GetValue(i));
bli.SzDesc = names;
blc.Add(bli);
}
--
Share The Knowledge. I need all the help I can get and so do you!


Family Tree Mike said:
YankeeImperialistDog said:
Thanks for replying,

My issue is i'll be passing in a string that represents the enum not the
enum itself. What i'd like to do is get to the enum using a string.
my solution so far is:
[DataObjectMethod(DataObjectMethodType.Select, true)]
public static BasicListCollection eNumProductTypeDDL(string
enumName)
{
Type typ = null;
switch (enumName.ToLower())
{
case "producttarget":
typ = typeof(ProductTarget);
break;
case "producttype":
typ = typeof (ProductType);
break;
case "billinginterval":
typ = typeof (BillingInterval);
break;
case "billingcycle":
typ = typeof(BillingCycle);
break;
case "billingtype":
typ = typeof(BillingType);
break;
case "trialperiod":
typ = typeof(TrialPeriod);
break;
default:
typ = typeof (PlaceHolder);
break;
}

string[] names = Enum.GetNames(typ);
Array values = Enum.GetValues(typ);
var blc = new BasicListCollection();
for (int i = 0; i < names.Length; i++)
{
var bli = new BasicList();
bli.IID = Convert.ToInt32(values.GetValue(i));
bli.SzDesc = names;
blc.Add(bli);
}
return blc;
}

but this is still hard coding a test for the enum. I'd like to find a way
to
make it generic so to speak where i can maybe use reflection to get the
emun?
not sure if this is the way or not.

I appreciate you advice and any additional light you can shed on this.

thanks


Oh, now I see. Try:

Type typ = Type.GetType(enumName, true, false);

You need to pass the fully qualified name for the enume however, which is
probably something like "MyProject.BillingType".

http://msdn.microsoft.com/en-us/library/a87che9d.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top