0

Is there a way to basically do this:

string x = "hi";
int hi = 3;
Console.WriteLine([x].ToString());

get what I'm trying to do? I want to print "3", not "hi." I want to use x to reference to hi. how might I do this?

3
  • 5
    Why do you want to do this? It sounds like you should use a Dictionary<string,int>. Commented Mar 12, 2011 at 3:44
  • You could use reflection for that. Granted, not for local variables (correct me if I'm wrong?). Commented Mar 12, 2011 at 3:47
  • 2
    Consider move away from PHP and learn real programming techniques, read Matts comment and rcravens answer. Commented Mar 12, 2011 at 3:55

2 Answers 2

5

What about a Dictionary.

Dictionary<string, int> data = new Dictionary<string, int>();
data["hi"] = 3;

Console.WriteLine(data["hi"]); // prints 3
Sign up to request clarification or add additional context in comments.

1 Comment

Should int and string be reversed in your example to accomplish what he wants? Or am I just being dense?
0

You could try (sorry i'm typing from my phone)

class MyClass
{

  public int x {get;set;}
  MyClass()
  {
      x = 3;
  }

  void Foo()
  {
      Type type = GetType();
      PropertyInfo pInfo = type.GetProperty("x");

      object xValue= pInfo.GetValue(this, null);
      Console.Writeln(xValue); 
  }


}

Comments

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.