Is it possible to create a variable from a function?


Is it possible to create a function that creates variables?

For example something like:


would make the “customCreateVariables” function create:

var coolguy:String;

Basically I want to create certain variables with specific variable types through a function.

Thank you in advance for any reply or point to the right direction.

Thank you :slight_smile:

Figured I might make it easier to understand what I am trying to accomplish…

I have 3 sections in my flash file. Only 1 section shows at a time.

Via AS3 I am creating CheckBox objects, and placing them on individual sections. Instead of creating each checkbox individually, I want to be able to create a function, tell it how many checkboxes to create, and also what name to give each checkbox.

Actually looking at my code, I think I have something similar to what I am trying to achieve, but not sure if it is the right way to go about it…

For example I am using this code:

var tab1_gp2_sub1_Options:int = 10;
for (i=0; i<=tab1_gp2_sub1_Options; i++)
                //Option Creation Loop
                tab1_gp2_sub1_op = new CheckBox();
       = "tab1_gp2_sub1_op" + i;

                //Add Properties for Options
                tab1_gp2_sub1_op.label = tab1_gp2_sub1_op_Labels[i];
                tab1_gp2_sub1_op.width = 200;

What the above will do is create objects tab1_gp2_sub1_op0 through tab1_gp2_sub1_op9 and assign the above values to each one.

So Maybe I can do something like:

createVariables("tab1_gp2_sub1_op", 10);

function createVariables(InstanceName:String, vCount:Number):void{
for (i=0; i<=vCount; i++)
                //Option Creation Loop
                var newCheckbox:CheckBox = new CheckBox();
       = InstanceName + i;

                //Add Properties for Options
                newCheckbox.label = "Checkbox: " + i;
                newCheckbox.width = 200;

Would the above function do the same job as the first For Loop I posted, but create a checkbox with the InstanceName I passed to the function?

Ummm, kinda… but it’s not the greatest approach.

If you’re going to be making a collection of values, you should use an Array (or better yet, a Vector).

So, for example, if you needed to make 10 check boxes, something like this would work.

// Somewhere that creates the variable in a place the function can find.
// Alternatively you could pass the variable to the function.
var checkboxes:Vector.<CheckBox> = new Vector.<CheckBox>();

function makeCheckBoxes() {
    for(var i:int = 0; i < 10; i++) {
        var newCheckbox:CheckBox = new CheckBox();
        // Add it to the vector.

// access it somewhere
checkboxes[0].name = "hello";

If you aren’t familiar with Vectors, they’re essentially the same as Arrays, except they can only hold a single filetype (which you specify in the <>). Doing this allows them to have a pretty substantial performance boost. They have all the same functions otherwise as an array.

Hope that helps.