2

I want to get the value of a variable from its name.

To clarify, an XML document delivers the variable name as a string; I want to get the value.

Something like this:

string bublegumA = "strawberry";
string bubblegumB = "banana";   

//get which variable from the XML

string fromXML = "bubblegumB";

string output = getValue(fromXML);

//it should return "banana"
1
  • 1
    "Using c# .net 2.0 I want to get the value of a variable from it's name." - No you don't, you want a real solution to this problem, you just can't imagine it yet. Commented Apr 23, 2013 at 23:59

2 Answers 2

4

You can't do that. Variable names only exist at design time. Once it's compiled (without debugging symbols) the variable name is lost.

But you can do this:

var myValues = new Dictionary<string, string>();
myValues["bublegumA"] = "strawberry";
myValues["bublegumB"] = "banana";

string output = myValues["bublegumB"]; // "banana"

You could also use an ExpandoObject if you don't like the dictionary syntax for setting properties, though you still have cast it to a dictionary to get an value by name:

dynamic myValues = new ExpandoObject();
myValues.bublegumA = "strawberry";
myValues.bublegumB = "banana";

string output1 = myValues.bublegumB; // "banana"
string output2 = (string)((IDictionary<string, object>)myValues)["bublegumB"]; // "banana"
Sign up to request clarification or add additional context in comments.

4 Comments

I know you CAN do that using Reflection.
@WayneScroggins With variables, you can't. Reflection allows you to inspect the structure of types, including properties and fields (like RussC's answer) but the names of variables defined within a method do not exist at run-time.
@WayneScroggins Also note that reflection is much slower than using a collection. If you're just using this as way to store name-value pairs, it's really not a good solution.
@WayneScroggins FWIW, I included an example using ExpandoObject which offers a different syntax, but essentially uses a dictionary to store data internally.
2

Basically, this is an application of reflection:

This should work, if it's a field:

var value = targetObject.GetType().GetField(fromXml).GetValue(targetObject, null);

So if your class is:

public class MyClass
{
    public string BubblegumA = "Strawberry";
}

then in your main class:

public static void Main()
{
    MyClass targetObject = new MyClass();

    var value = targetObject.GetType().GetField("BubblegumA").GetValue(targetObject, null);

    //value should = Strawberry
}

5 Comments

I would recommend against advising a beginner to use reflection when a lookup table will do.
I agree, if this was a problem I was trying to solve, it's not the way I'd do it - but it is a valid answer to the problem - just not best practice.
@Valamas-AUS: Sometimes the right answer is not on which directly addresses the request, as is the case here.
I am far from a beginner. I am looking for how to do this, not how to use a dictionary. Reflection would be nice to use, if it does what I need.
The Reflection example, is nice, and is something I have been trying to use. Still, it seems to be "cranky" at me when I try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.