Re: Reflection and variable selection? Late binding?



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








.



Relevant Pages

  • RE: Have some problem with enum and string
    ... I have below a for loop and a switch in the for loop. ... I have also a enum called colBlowStep with some values. ... I know I can use string but I rather want to use enum. ...
    (microsoft.public.dotnet.languages.csharp)
  • Why cant switch be used for objects
    ... The Jit could certainly optimize this case because the addresses of static ... The thing is that is often used my own enum classes because I usually want ... public Color[] GetGreenLikeColors.. ... I see no technical reason why switch shouldn' be allowed for objects. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: switch on string value
    ... I think Bob has a really good point: ... Switch seems to me ... then maybe making them an enum in the first ... earlier it is better to convert your string to an enum ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: switch on string value
    ... I don't see the value of converting to an enum just to perform the switching. ... I've always been a fan of "switch" over "if / else if". ... earlier it is better to convert your string to an enum ... returns the unboxed enum; then doing a switch on the unboxed enum. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Have some problem with enum and string
    ... in not an int it's a string. ... I have below a for loop and a switch in the for loop. ... I have also a enum called colBlowStep with some values. ... I want to use enum in the case statment in the switch. ...
    (microsoft.public.dotnet.languages.csharp)