By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,455 Members | 1,385 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,455 IT Pros & Developers. It's quick & easy.

using a dynamic typeof question

P: n/a
i'm trying to modify some code to accept an enum then use reflection to fill
a dropdown list. If i name the type directly, replace enm with the real name
of the enum it works, no suprise. Question is can i make this dynamic. I
throws an error on run that it can't find the namespace. I understand why,
but is what i'm trying to do possible?

public static void BindEnumToDropDown(ref DropDownList ddl, Enum enm)
{
FieldInfo[] myEnumFields = typeof(enm).GetFields(); // ERRORS HERE
foreach (FieldInfo myField in myEnumFields)
{
if (!myField.IsSpecialName && myField.Name.ToLower() !=
"notset")
{
int myValue = (int)myField.GetValue(0);
ddl.Items.Add(new ListItem(myField.Name,
myValue.ToString()));
}
}
}
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes
Jul 11 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Fri, 11 Jul 2008 15:12:04 -0700, WebBuilder451
<We***********@discussions.microsoft.comwrote:
i'm trying to modify some code to accept an enum then use reflection to
fill
a dropdown list. If i name the type directly, replace enm with the real
name
of the enum it works, no suprise. Question is can i make this dynamic. I
throws an error on run that it can't find the namespace. I understand
why,
but is what i'm trying to do possible?
I think if you changed "typeof(enm)" to "enm.GetType()" it would work.
However, since the Enum class already has a nice GetNames() method to
return an array of the enum names, it seems to me that would be a better
approach than using reflection.

Pete
Jul 12 '08 #2

P: n/a
thanks for responding.
if i use enm.GetType() it will not compile and show mismatched paren's in
intelesense.
your are correct that the GetNames works just fine, however, i don't get the
numeric values, hense the use of reflection.

Thanks
KES
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes
"Peter Duniho" wrote:
On Fri, 11 Jul 2008 15:12:04 -0700, WebBuilder451
<We***********@discussions.microsoft.comwrote:
i'm trying to modify some code to accept an enum then use reflection to
fill
a dropdown list. If i name the type directly, replace enm with the real
name
of the enum it works, no suprise. Question is can i make this dynamic. I
throws an error on run that it can't find the namespace. I understand
why,
but is what i'm trying to do possible?

I think if you changed "typeof(enm)" to "enm.GetType()" it would work.
However, since the Enum class already has a nice GetNames() method to
return an array of the enum names, it seems to me that would be a better
approach than using reflection.

Pete
Jul 12 '08 #3

P: n/a
WebBuilder451 wrote:
i'm trying to modify some code to accept an enum then use reflection to fill
a dropdown list. If i name the type directly, replace enm with the real name
of the enum it works, no suprise. Question is can i make this dynamic. I
throws an error on run that it can't find the namespace. I understand why,
but is what i'm trying to do possible?

public static void BindEnumToDropDown(ref DropDownList ddl, Enum enm)
{
FieldInfo[] myEnumFields = typeof(enm).GetFields(); // ERRORS HERE
foreach (FieldInfo myField in myEnumFields)
{
if (!myField.IsSpecialName && myField.Name.ToLower() !=
"notset")
{
int myValue = (int)myField.GetValue(0);
ddl.Items.Add(new ListItem(myField.Name,
myValue.ToString()));
}
}
}
The typeof keyword only works with a type name, not a variable. Use
enm.GetType() to get the type.

--
Göran Andersson
_____
http://www.guffa.com
Jul 12 '08 #4

P: n/a
On Fri, 11 Jul 2008 19:11:00 -0700, WebBuilder451
<We***********@discussions.microsoft.comwrote:
thanks for responding.
if i use enm.GetType() it will not compile and show mismatched paren's in
intelesense.
Could you post the code that's not compiling? I don't understand your
description.
your are correct that the GetNames works just fine, however, i don't get
the
numeric values, hense the use of reflection.
You can easily convert a name to a value using the Enum.Parse() method.

Pete
Jul 12 '08 #5

P: n/a
On Jul 12, 7:42*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Fri, 11 Jul 2008 19:11:00 -0700, WebBuilder451 *

<WebBuilder...@discussions.microsoft.comwrote:
thanks for responding.
if i use enm.GetType() it will not compile and show mismatched paren's in
intelesense.

Could you post the code that's not compiling? *I don't understand your *
description.
I think he's trying to do typeof(enm.GetType()) ...

It needs to be just enm.GetType() - typeof() is strictly compile-time.
your are correct that the GetNames works just fine, however, i don't get *
the
numeric values, hense the use of reflection.

You can easily convert a name to a value using the Enum.Parse() method.
No need for that, even. In addition to Enum.GetNames(), there's also
Enum.GetValues() - and they both return elements sorted by value, so
you can just zip those two pairwise and get name-value pairs.
Jul 12 '08 #6

P: n/a
Thank you,
This was indeed an issue of assuming more flexible means more complicated.

KES
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes
"Göran Andersson" wrote:
WebBuilder451 wrote:
i'm trying to modify some code to accept an enum then use reflection to fill
a dropdown list. If i name the type directly, replace enm with the real name
of the enum it works, no suprise. Question is can i make this dynamic. I
throws an error on run that it can't find the namespace. I understand why,
but is what i'm trying to do possible?

public static void BindEnumToDropDown(ref DropDownList ddl, Enum enm)
{
FieldInfo[] myEnumFields = typeof(enm).GetFields(); // ERRORS HERE
foreach (FieldInfo myField in myEnumFields)
{
if (!myField.IsSpecialName && myField.Name.ToLower() !=
"notset")
{
int myValue = (int)myField.GetValue(0);
ddl.Items.Add(new ListItem(myField.Name,
myValue.ToString()));
}
}
}

The typeof keyword only works with a type name, not a variable. Use
enm.GetType() to get the type.

--
Göran Andersson
_____
http://www.guffa.com
Jul 12 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.