Re: Reflection and variable selection? Late binding?
- From: "Dave Sexton" <dave@jwa[remove.this]online.com>
- Date: Fri, 15 Sep 2006 16:52:06 -0400
Hi Ben,
No reflection or binding needed:
string text = null;
switch (variableName)
{
case "foo":
text = foo.ToString();
break;
case "bar":
text = bar.ToString();
break;
}
MessageBox.Show(text);
I'm not sure it's such a good idea to pass in the name of a local variable though. An Enum would be appropriate here instead:
enum DoSomethingChoice
{
Foo,
Bar
}
class FooBarChoiceTest
{
int foo = 1, bar = 2;
void DoSomething(DoSomethingChoice choice)
{
string text = null;
switch (choice)
{
case DoSomethingChoice.Foo:
text = foo.ToString();
break;
case DoSomethingChoice.Bar:
text = bar.ToString();
break;
default:
throw new System.ComponentModel.InvalidEnumArgumentException(
"choice", (int) choice, typeof(DoSomethingChoice));
}
MessageBox.Show(text);
}
}
--
Dave Sexton
"Ben R." <benr@xxxxxxxxxxxxxxxx> wrote in message news:AF6EED6C-7C1C-4C7B-93D2-2A01231C7552@xxxxxxxxxxxxxxxx
Hi,
I learned that for my.sttings, I can pull an entry by name, but can I use
reflection to load a varaible? Example
void dosomething(string variablename)
// note: value passed in for variable name is "foo"
int foo=1
int bar=2
messagebox.show(reflectionhere(variablename));
I want 1 to show up.
Is this possible? Is this what late binding is? There are circumstances
where it'd be nice to construct a string which will represent a varaible name
that I'd like to use.
Thank you...
-Ben
.
- Follow-Ups:
- Re: Reflection and variable selection? Late binding?
- From: Ben R.
- Re: Reflection and variable selection? Late binding?
- Prev by Date: Re: searching a Combo box for a Generic item
- Next by Date: Re: GAC
- Previous by thread: Addrow to DataGridView
- Next by thread: Re: Reflection and variable selection? Late binding?
- Index(es):
Relevant Pages
|