Jump to content
  • Advertisement
Sign in to follow this  
_Danneman_

Variable variables in C#

This topic is 4614 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In for instance ActionScript (programs flash) there is a possibility of calling a variable by typing a part of its name, and add the rest of the name as the content of another variable. Ex:
var_1 = "Hello";
var_2 = 1;
var_3 = _root["var_" + var_2];

// var_3 gets the value from var_1, ie. "Hello"
Does anything like that exist in C#?

Share this post


Link to post
Share on other sites
Advertisement
You can do it through the reflection API, check out the System.Reflection namespace. I don't know if there's an easier way.

Share this post


Link to post
Share on other sites
Hm, there might be an easier way, as I don't know C# well, but you can use arrays in the situation you have there. Something like:

(c++)

//create array
array[0] = "hello";

int index = 0;

myVar = array[index];


This will accomplish the same thing as you posted, but will not be fun or organized to have several variables each in an array like that.

Share this post


Link to post
Share on other sites


Type t = theObject.GetType();
// field is a member variable
t.GetField("FieldName").GetValue(theObject));
// or get property, which is a property defined like
// int SomeProperty { get{ return 0; } }
t.GetProperty("PropertyName").GetValue(theObject, null));



Share this post


Link to post
Share on other sites
Does ActionScript have arrays, even? o_O

If you really need to be able to map from a "name" to some "value", then use - a map.


#include <map>
#include <string>
//...

std::map<std::string, std::string> vars;
vars["1"] = "Hello";
vars["2"] = "1"; // can't store an int. C++ is statically typed.
vars["3"] = vars[vars["2"]];

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by Zahlman
*** source code removed ***

Psst. C#. Not C++.

I'm pretty sure the corresponding class is System.Collections.Generic.Dictionary<Key, Value>

CM

Share this post


Link to post
Share on other sites
Oops. (Don't know enough C# to help, and yeah, obviously I've been helping way too many people with C++ in a row... come on people, use a more friendly language :) )

Share this post


Link to post
Share on other sites
A possible way for this to work is definately an array, here is some sample code if you're still having trouble.

string[] var;
var = new string[];

var[1] = "Hello";
var[2] = "1";
var[3] = var[1] + var[2];

Yes using that code var[2] won't be and integer so if you were to add numbers in that array 1 + 2 = 12 so be careful about that. If you need more help just PM me or something.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!