Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

actionscript: storing objects in an array

Status
Not open for further replies.

BluesmanUK

Programmer
Dec 17, 2001
55
GB
bit of a programming question here :
I have a data object class in which I would like a property to be an array of objects , each with their own properties and methods. It seems that in actionscript , once put inside an array, an object loses its type - does anyone know if is there any way round this?

Would it be bad to just create a generic object for each array element and dynamically assign properties to it?

 
Code:
var someFunction:Function = function ():String {
	return "boo";
};
var arrayWithObject:Array = [{prop:"val1", func:someFunction}, {prop:"val2"}];
trace(arrayWithObject[1].prop);
trace(arrayWithObject[0].func());

val2
boo

Kenneth Kawamoto
 
Ah i see what you're doing there - but can you type prop, and can func() access prop?

Im writing an application which includes a slideshow with the content coming from xml - so ive created a data class to hold the xml data, and i wanted an array of 'slide' objects as a property of that data class. Each slide object itself would have properties such as:

image path (string)
text (array of text strings)
pause time (number)

with methods for returning the image path , text array etc

my problem is that once the slide object has been pushed into the array, it loses its typing, and so do its properties. Therefore if there are errors in the XML from which all this data comes from , the application will fail silently at compile time and at runtime.

This wouldnt be so much of a problem , but there is the potential that there will be non-technical people editing the xml file :-(


 
Code:
var someFunction:Function = function ():String {
	return this.prop;
};
var arrayWithObject:Array = [{prop:"val1", func:someFunction}, {prop:"val2"}];
trace(arrayWithObject[0].func());
val1

...you can put anything in Object and also put anything in Array.

Kenneth Kawamoto
 
yeah but my point is that you lose the benefit of Typing when you put Objects into an Array...
 
You need to type cast the Object if you are retrieving it from an Array using [].

Imagine an Object with a property "someNumber", which is a Number.

This is what happens if you don't do type casting:
Code:
var someObject0:SomeObject = new SomeObject();
var arrayWithObjects:Array = [someObject0];
arrayWithObjects[0].someNumber = "I am not a number";
trace(arrayWithObjects[0].someNumber);
I am not a number

...this should not happen as "someNumber" is a Number and you should not be able to assign a String. I think this is what you mean by "loses type".

But if you do type casting you'll get a different story:
Code:
var someObject0:SomeObject = new SomeObject();
var arrayWithObjects:Array = [someObject0];
SomeObject(arrayWithObjects[0]).someNumber = "I am not a number";
**Error** Scene=Scene 1, layer=actions, frame=1:Line 3: Type mismatch in assignment statement: found String where Number is required.
SomeObject(arrayWithObjects[0]).someNumber = "I am not a number";

Total ActionScript Errors: 1 Reported Errors: 1

...Error because you can't assign a String to a Number.

Kenneth Kawamoto
 
Brilliant, thats just what I meant :)
Thanks to your info i've written a class that stores an array of objects and type casts every time i retrieve the array element - this seems to work perfectly well:

Code:
import com.mydomain.Page;
class com.mydomain.PageCollection {
	
	private var myArray:Array;
        
  // constructor
	public function PageCollection() {
		
		myArray = new Array();
		
	}

	public function addPage(obj:Page):Void {
		
		myArray.push(obj);
		
	}

	public function getPage(index:Number):Page {
		
        // type cast the object!
		return Page(myArray[index]);
		
	}
	
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top