0

I need to create a variable:

var numDots0:Number=0;

But when a button is clicked the variable numDots0 becomes numDots1, then numDots2 on a second click, and so on. I then need to be able to grab that new variable name and use it in a function.

2
  • 5
    I really doubt this is actually what you need. A variable that changes name is not something that leads to readable code. What are you trying to accomplish? Commented Apr 18, 2012 at 1:44
  • Yes, please, context will help you receive much better advice. Commented Apr 18, 2012 at 2:19

3 Answers 3

2

That's a really, really weird request, but anyways:

You can use the key name of an Object to store the property and then change that:

var obj:Object = { numDots0: 0 };

And then when you want to change the name:

delete obj.numDots0;
obj.numDots1 = 1;

Or to easily increment you can use this:

var i:int = 0;
function increase():void
{
    delete obj["numDots" + i];
    obj["numDots" + (++i)] = i;
}

To access:

trace(obj.numDotsX); // where X is the most recent variable name.

I see absolutely no benefit or need for this, so I strongly suggest taking a look at what you're trying to do and making sure it makes sense and doesn't have a different application.

Sign up to request clarification or add additional context in comments.

3 Comments

Well as Im sure you've assumed my actionscript is not the strongest, but I'll try better explain what Im attempting to do, and post my code.I have an application that allows you to draw shapes by placing points on a grid and connecting the dots as it goes, what I want is to adapt a button into the existing code that allows the creation of a new shape.
@ejn There are ways to access dynamically created instances without assigning them to a variable, for example you can add them into an array.
You can get them from the display tree as well
0

I am pretty sure you are going the wrong way about the problem you are trying to solve. Dynamic variable names are not something you read in the best practices book.

Anyway to answer your question in AS2 you could use the command eval which would evaluate a string as ActionScript, so you would use something like:

function onClicked(e:MouseEvent):void
{
     counter++;
     eval("var numDots" + counter +"+:Number=0;");
}

In AS3 that command has been removed (because it leads to bad coding practices - like the things you are trying to do), nevertheless someone implemented an evaluator in AS3:

http://eval.hurlant.com/

With this evaluator add the library to your project and add the following to the snippet above:

function eval(expression:String):void
{
    var evaluator:com.hurlant.eval.Evaluator = new com.hurlant.eval.Evaluator();
    var bytes:ByteArray = evaluator.eval(expression);
    bytes = ByteLoader.wrapInSWF([bytes]);

    var context:LoaderContext = null
    var loader:Loader = new Loader();

    loader.loadBytes(bytes, context);
}

Comments

0

the answer is to not do what you are trying to do and use an array, hash or vector instead. give us a bit more context, or the reason you want to achieve exactly what you want to and why you might believe you'd need a dynamic variable name like that. you shouldn't be using evals or anything that changes variable name at runtime because the gods of programming will strike you down where you stand. i.e., your program is going to break, and when it does, it's going to be harder to debug for sure.

if you are sure this is what you want to do, then i'm wrong, haha. good luck!

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.