# Problems with dynamic variable names

**URL:** <https://forum.kirupa.com/t/problems-with-dynamic-variable-names/64003>\
**Category:** Uncategorized\
**Created:** [September 12, 2004, 1:17pm UTC](https://forum.kirupa.com/t/problems-with-dynamic-variable-names/64003 "2004-09-12T13:17:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![OM2](https://avatars.discourse-cdn.com/v4/letter/o/3ab097/32.png) [@OM2](https://forum.kirupa.com/u/OM2)\
**Post date:** [September 12, 2004, 1:17pm UTC](https://forum.kirupa.com/t/problems-with-dynamic-variable-names/64003/1 "2004-09-12T13:17:15Z")

</div>

I want to write code where I have arrays of objects.  
I want to write a for loop and have something like:

[size=2][size=2]myObject1 = new Object();  
[/size][/size][size=2][size=2]myObject2 = new Object();  
[/size][/size][size=2][size=2]myObject3 = new Object();  
[/size][/size][size=2][size=2]myObject4 = new Object();  
etc…  
[/size][/size]  
How can I do this?  
I’ve tried using square brackets… it doesn’t work and looks wrong to do anyway.

Any help would be appreciated.

Thanks.

OM

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 12, 2004, 1:54pm UTC](https://forum.kirupa.com/t/problems-with-dynamic-variable-names/64003/2 "2004-09-12T13:54:30Z")

</div>

//declaring a new Array  
var aValues:Array = new Array();

//adding the data to the Array with array access notation method  
aValues[0] = myObject1;  
aValues[1] = myObject2;  
aValues[2] = myObject3;

//returning every index in the array from zero (the first object) to the length of the array, so we don’t have to hardcode the length of the array in the for statement.  
for (var i:Number = 0; i \< aValues.length; i++);  
trace(aValues\*);

I hope that helps a bit 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 12, 2004, 1:55pm UTC](https://forum.kirupa.com/t/problems-with-dynamic-variable-names/64003/3 "2004-09-12T13:55:21Z")

</div>

myObject = new Array();  
myObject[0] = new Object(); // arrays start on 0  
myObject[1] = new Object();  
myObject[2] = new Object();  
myObject[3] = new Object();

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 12, 2004, 4:39pm UTC](https://forum.kirupa.com/t/problems-with-dynamic-variable-names/64003/4 "2004-09-12T16:39:48Z")

</div>

I have a similar question…  
Would it at all be possible to do something like this?

```auto
 
var i = 0;
while (i <=10) {
   myTextField*.text = "Textfield:" +i+"";
   i++;
}

```

The part I can’t get working is “myTextField\*.text” and I don’t even know if this kind of thing is possible. Anyone know if it is? And what you should use instead of “\*”?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 12, 2004, 5:22pm UTC](https://forum.kirupa.com/t/problems-with-dynamic-variable-names/64003/5 "2004-09-12T17:22:59Z")

</div>

Ok, I found the solution to my problem. Just adding how I did it if any other newbie wonders what it was.

```auto
 
var i = 0;
while (i <=10) {
   _root["myTextField"+i].text = "Textfield:" +i+"";
   i++;
}

```
