Re: Reflection and variable selection? Late binding?
- From: "Dave Sexton" <dave@jwa[remove.this]online.com>
- Date: Sat, 16 Sep 2006 16:46:21 -0400
Hi Ben,
If your variables are all integers (or any of the supported base Types for Enums) then use an Enum as in my second example. Avoid
the switch by casting the specified enum value to its base Type and assign each Enum constant the appropriate value:
enum DoSomethingChoice
{
Foo = 1,
Bar = 2
}
void DoSomething(DoSomethingChoice choice)
{
int value = (int) choice;
MessageBox.Show(value.ToString());
}
If your variables are other Types such as strings or complex Types then you should use the switch statement. If you have a lot of
variables then you might want to think of a better design. You could store the data in a Hashtable keyed by the DoSomethingChoice
enum, for example.
You could also expand the method into a more OOP design so that the argument passed to the method is a custom object that
encapsulates all of the data that the method might require.
--
Dave Sexton
"Ben R." <benr@xxxxxxxxxxxxxxxx> wrote in message news:96B6BD0B-C7DE-4810-A577-40ADBB0C0B08@xxxxxxxxxxxxxxxx
Hey Dave,
Thanks for the response. I was thinking about putting in the switching logic
you suggested, but I'm wondering if this can be avoided. I'm considering a
situation where I have many different variables that could be used, instead
of just 2. It seems silly to have to write switching logic for each variable
when each case is doing something very similar: going from string "x" to
variable x.
Is there any way you know of to accomplish this without explicit switching
logic?
-Ben
"Dave Sexton" wrote:
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
.
- References:
- Re: Reflection and variable selection? Late binding?
- From: Dave Sexton
- Re: Reflection and variable selection? Late binding?
- From: Ben R.
- Re: Reflection and variable selection? Late binding?
- Prev by Date: Re: Variable length arrays in .NET 2.0?
- Next by Date: Re: Question on creating a shared dell from PC to PDA
- Previous by thread: Re: Reflection and variable selection? Late binding?
- Next by thread: RE: Reflection and variable selection? Late binding?
- Index(es):
Relevant Pages
|