Reflection and variable selection? Late binding?

G

Guest

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
 
D

Dave Sexton

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);
}
}
 
G

Guest

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
 
D

Dave Sexton

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.
 
W

Walter Wang [MSFT]

Hi Ben,

It's possible to use Reflection to get/set the variable value:

public int foo = 1;
public int bar = 2;
private int privateFoo = 1;

public void dosomething(string variablename)
{
Type t = this.GetType();
FieldInfo fi = t.GetField("privateFoo", BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.Public);
Console.WriteLine(fi.GetValue(this));
}

From:

#Early and Late Binding
http://msdn2.microsoft.com/en-us/library/0tcf61s1.aspx

The Visual Basic compiler performs a process called binding when an object
is assigned to an object variable. An object is early bound when it is
assigned to a variable declared to be of a specific object type. Early
bound objects allow the compiler to allocate memory and perform other
optimizations before an application executes.

By contrast, an object is late bound when it is assigned to a variable
declared to be of type Object. Objects of this type can hold references to
any object, but lack many of the advantages of early-bound objects.

C# does not handle late binding.

For late binding related information, please refer to:

#Dynamically Loading and Using Types
http://msdn2.microsoft.com/en-us/library/k3a58006.aspx

Please reply here to let us know whether or not you need further
information. Thank you.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Walter,

Thanks! This is great info. You've also inspired me to dig deeper into
custom binding..

-Ben
 
W

Walter Wang [MSFT]

Hi Ben,

I would be more happier if you had replied earlier, :) Just kidding,
anyway, thank you for letting me know that the suggestion helped.

Have a nice day!

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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