Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

how to dynamically create array names?

Contributor ,
Jun 17, 2010 Jun 17, 2010

i need to dynamically create arrays with variable array names.

for example, something like this:

for (var n=0 ; n < nodeContainer.length ; n++) {

     nodeObj = nodeContainer;

     var name:String = nodeObj.nodeID;

     array[name]:Array = new Array(); // will create the array as "arrayName"?

}

              

how? 

TOPICS
ActionScript
7.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jun 18, 2010 Jun 18, 2010

Although MovieClip is a dynamic class, classes that extend it DO NOT INHERIT dynamic features.

You would do yourslf a big favor if you read documentation:

http://www.adobe.com/livedocs/flex/201/langref/statements.html#dynamic

In order for a class to be dynamic it must be explicitly set as such. In your case it should be:

public dynamic class Torque extends MovieClip

And, again, you REALLY need to get understanding of AS3 syntax. I am sorry for lecturing you but in spite of suggestions you do insist o

...
Translate
LEGEND ,
Jun 17, 2010 Jun 17, 2010

What is array variable? Is it Array? If so, you cannot do that.

You need to use a dynamic class like Object or look into Dictionary class - it may be what you want.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 17, 2010 Jun 17, 2010

yes, array var is Array Class

corrected and clarified:

for (var n=0 ; n < nodeContainer.length ; n++) {

     nodeObj = nodeContainer;

     var name:String = nodeObj.nodeID; // will be something like "Media"

     var array[name]:Array = new Array(); // will create the array as "arrayMedia"?

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 17, 2010 Jun 17, 2010

Again, Array is not a dynamic class, so you cannot add names or any other custom properties/methods to its instance.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 17, 2010 Jun 17, 2010

okay, so, i think i can use objects then.  yet how to dynamically assign a string to the object name?

something like this?

for (var n=0 ; n < nodeContainer.length ; n++) {

     nodeObj = nodeContainer;

     var name:String = nodeObj.nodeID; // will be something like "Media"

     var obj[name]:Object = new Object(); // will create the array as "objMedia"?

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 17, 2010 Jun 17, 2010

okay, yes : thanks for the reminder of Vector, as i think that is the way to go.

yet, even after looking at the documentation for the Vector class, i do not see how to dynamically assign the name of the Vector var instance.

something like this?

for (var n=0 ; n < nodeContainer.length ; n++) {

     nodeObj = nodeContainer;

     var name:String = nodeObj.nodeID; // will be something like "Media"

     var v[name]:Vector.<Object> = new Vector.<Object>(12); // will create the vector as "vMedia"?

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 17, 2010 Jun 17, 2010

thanks for your help!!

to be clear : my primary question here is : HOW CAN I DYNAMICALLY NAME A VAR (be it an Object or a Vector) which I can then put other objects inside.  i'm working with dynamic data, so the names of the data nodes will change in name and number, so being able to create custom named organizational containers for sets of data is key.

the Vector has many properties which might be useful in my application--yet the critical thing is that i need to be able to name the variable upon the moment it is created, and then reference those variables elsewhere ...

ie:

VectorExample1 (created from string "Example1")

VectorCats (created from string "Cats")

VectorDogs (created from string "Dogs")

VectorWhatever (created from string "Whatever")

etc ...

then elsewhere:

thisCat = VectorCats[7];

how???

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 17, 2010 Jun 17, 2010

ah, something like this?:

for (var i:int=0; i<3; i++) {

     this["myVar_"+i] = new Vector.<Vector.<int>>();

     for (var j:int=0; j<3; j++) {

          this["myVar_"+i] = new Vector.<int>();

          for (var k:int=0; k<3; k++) {
               this["myVar_"+i]=k;
          }
     }
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 17, 2010 Jun 17, 2010

the above is supposed to work (found it on-line), yet i've adapted it to be simpler, and even still i get a error message when using this:

for (var n=0 ; n < nodeContainer.length ; n++) {

     nodeObj = nodeContainer;

     var vName:String = nodeObj.nodeID;

     this["v_"+vName] = new Vector.<Object>();

     trace (["v_"+vName])

}

error message:

"ReferenceError: Error #1056: Cannot create property v_root on Torque.Torque."

so it appears that the method of creating the dynamic Vector name is valid ("root" string is coming from the data object), yet why will it not create it?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 17, 2010 Jun 17, 2010

anyone know how to do this?

i am searching on-line, yet not finding anything else ...

i'm running out of time (project deadline) and this is totally holding me up ....

thanks!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 17, 2010 Jun 17, 2010

anyone? ..

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 18, 2010 Jun 18, 2010

Look, you need to polish the understanding of AS objects...

As I said, you can create properties at runtime ONLY on DYNAMIC objects. In AS3 there are at least three natively dynamic classes: Object, MovieClip, and Dictionary. Obviously, Torque.Torque, which is referred with the keyword "this" IS NOT A DYNAMIC OBJECT either.

One thing you can do is:

var myObject:Object = new Object();

for (var n:int = 0 ; n < nodeContainer.length ; n++) {
     nodeObj = nodeContainer;
     var vName:String = nodeObj.nodeID;
     myObject["v_"+vName] = new Object();// or whatever. It can be anything, including Array
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 18, 2010 Jun 18, 2010

thanks!!  yes, i am polishing understanding ...

the Class this is going into is an extension of MovieClip, so it should be DYNAMIC:

public final class Torque extends MovieClip {



i tried this:

var myObject["v_"+vName] = new Object();



yet get this error:

1086: Syntax error: expecting semicolon before leftbracket.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 18, 2010 Jun 18, 2010

appears to just be a matter of the proper syntax?

using Vector class is a good idea for many reasons, so i've tried this also:

this["v_"+vName]:Vector.<Object> = new Vector.<Object>();



yet get this error:

1078: Label must be a simple identifier.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 18, 2010 Jun 18, 2010

Although MovieClip is a dynamic class, classes that extend it DO NOT INHERIT dynamic features.

You would do yourslf a big favor if you read documentation:

http://www.adobe.com/livedocs/flex/201/langref/statements.html#dynamic

In order for a class to be dynamic it must be explicitly set as such. In your case it should be:

public dynamic class Torque extends MovieClip

And, again, you REALLY need to get understanding of AS3 syntax. I am sorry for lecturing you but in spite of suggestions you do insist on doing things wrong although solutions are presented to you in the clearest way possible.

The line var myObject["v_"+vName] = new Object(); is a total nonsense in terms of the syntax. If you read my previous post(s), you would write it this way:

var myObject:Object = new Object();

myObject["v_"+vName] = new Object();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 18, 2010 Jun 18, 2010
LATEST

Thank you!!

I will take a look at that documentation.

Yes, I had already got some alternate advice, and changed my Class to this:

public final dynamic class Torque extends MovieClip {

Yet, the exact syntax of the dynamic Vector instantiation was still not working ... ?

Thankfully, I then got some very direct assistance with the Vector syntax ( http://forums.adobe.com/message/2907219#2907219 ), and now I know how to do it:

this["v_"+vName] = new Vector.<Object>();

// OR

var obj:Vector.<Object> = new Vector.<Object>();

this["v_"+vName]=obj;

So it is SORTED!

I'll keep this in mind also, as it is generally useful:

var myObject:Object = new Object();

myObject["v_"+vName] = new Object();

So, thanks again for your help and feedback.  I've been programming AS since 2000, and it sure is an on-going process of learning isn't it? This is my first time ever using the very new AS 3.0 Vector Class, so I'm sure glad for these community forums where we can help each other out ...

IMHO, your previous suggestions, while helpful, were not that explicit, nor as clear as possible (especially as I was enduring sleep depravation). My advice to you: polish up on rhetoric.

anyway, THANKS AGAIN SO MUCH!! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 17, 2010 Jun 17, 2010

In addition it is totally incorrect syntax in AS. If you want to restrict datatypes, you need to use Vector which is available in Flash 10.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines