addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscontroller-playcrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1light-bulblinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonprintShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

MadJS Message Board MadJS Discussion Forum › Variable Variable Question

Variable Variable Question

Christopher Edwin ...
chrisedjohn
Milwaukee, WI
Post #: 3
General programming question. Is there's way that I can use a variable only known during runtime to name a variable? Like the way you can create controls during runtime, but for declaring an array that I'll only know the name of later. A class? Ideas?
Ryan R.
user 79596502
Madison, WI
Post #: 3
Hi Chris,

I'm not sure if this is what you're looking for, but in JavaScript objects behave like hashes or "associative arrays". For example, you can reference the "bar" property of the "foo" object in a few different ways:





//set up our object and property
var foo = {};
foo.bar = 123;

//retrieve the value we set
alert(foo.bar);
alert(foo['bar']);
for(x in foo)
{
alert(foo[x]);
}






So, if you wanted to write or read a property of an object (associative array) at runtime, but you don't know the property name, you can do this:






//set up our example
var property1 = 'someRuntimeString';
var property2 = 'someOtherString';
var data = {};

//write values
myPropertyName = property1;
data[myPropertyName] = someValue;
myPropertyName = property2;
data[myPropertyName] = someOtherValue;

//read values
myPropertyName = property1;
alert(data[myPropertyName]); //value of someValue;
myPropertyName = property2;
alert(data[myPropertyName]); //value of someOtherValue;

//you can also now do this
alert(data.someRuntimeString);//value of someValue
alert(data.someOtherString);//value of someOtherValue






If you'd like to see this in action, check it out here: http://jsfiddle.net/x...­

All that being said, there is likely a different or simpler way to accomplish what you're trying to do. If you have a specific problem, feel free to post it and maybe the group can give you a different direction.


-Ryan
Christopher Edwin ...
chrisedjohn
Milwaukee, WI
Post #: 4
Thanks for that information, Ryan. After some searching, I believe what I'm after is sometimes called a dynamic variable. Looks like PHP has it http://php.net/manual/en/language.variables.variable.php­ but if I want to use in VB, I might have to use a dictionary work around: http://stackoverflow.com/questions/6966776/dynamic-variable-in-vb-net­
Ryan R.
user 79596502
Madison, WI
Post #: 4
Ah, sorry Chris. I misunderstood your question.

This is a message board for the MadJS (Madison JavaScript) meetup. If you have a general programming question that isn't JavaScript specific, I'd recommend trying StackOverflow or a forum dedicated to the language you're programming in.

Good luck!
-Ryan
Powered by mvnForum

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy