View Full Version : Initializing ArrayCollection?
bobobob
03-29-2010, 09:05 AM
so much of a noob i don't even know how to title the question...
what is the difference between
var example:ArrayCollection;
and
var example:ArrayCollection = new ArrayCollection();
thanx in advance
bob
sparX
03-29-2010, 10:23 AM
var example:ArrayCollection;
just says that u are declaring a variable with the identifier 'example' & the data type that will be stored against that variable will be of the 'ArrayCollection' type.
So all u got there is an empty container,labelled 'example',ready to throw some values that are of the 'ArrayCollection' data type into it.
var example:ArrayCollection = new ArrayCollection();
Now u are putting a value into the 'example' container & that value is a 'new' instance of the 'ArrayCollection' object & u are instantiating(constructing) that new instance by calling the constructor( ArrayCollection() ) in the 'ArrayCollection' class definition file.
So now everytime u look in the 'example' container,u will find that instance of the ArrayCollection object,or maybe more correct to say,everytime u want to refer to that 'ArrayCollection' object instance,u will use the reference 'example'.
bobobob
03-30-2010, 10:27 PM
thank you sparX for taking time to help.
let me restate the question.
what is the practical difference between
var exampleOne:ArrayCollection;
and
var exampleTwo:ArrayCollection = new ArrayCollection();
i.e. does exampleTwo take more space in memory?
are there assignments that can be made to exampleTwo that can not be made to exampleOne?
what are the circumstanses when i will want to use each?
sparX
03-31-2010, 05:39 AM
Maybe think of a var like an empty bucket.
In the first code example,u have gone & bought an empty bucket from the store & u have put a label on the empty bucket that says,
"this empty bucket is 'exampleOne',& u will only be able to throw an 'ArrayCollection' type object in this empty bucket should u happen to find one in future"
So u see the first code example u got there is just an EMPTY bucket with NO values stored in it & all it has on it is a label('exampleOne') & a warning(:ArrayCollection) telling someone that the only things they are allowed to store in the empty bucket is 'ArrayCollection' type objects?
In the second code example,what uve done is put a different label on a new empty bucket u just bought from the store('exampleTwo'),but on it is written the same warning that only 'ArrayCollection' type objects can be stored in this 2nd bucket.
BUT,also in the second code example,u have decided that u actually want to put a value(object) into this 2nd empty bucket so its not empty anymore,so u have decided to construct a 'new' instance of the 'ArrayCollection' type object( = new ArrayCollection(); ) & put this value into the bucket labelled 'exampleTwo'.(see edit below,its actually just a reference to the value, not the actual value in this case)
Furthermore,u are allowed to do this,because u are adhering to the warning u put on the bucket when it was empty,which says only ':ArrayCollection' type objects can be stored in this bucket labelled 'exampleTwo'.
So now 'exampleOne' is just still an empty bucket,u wont find no 'ArrayCollection' objects in there to play with.
But if u want to find your 'ArrayCollection' object instance that u have created,then u will find him in the now full bucket,labelled 'exampleTwo',so u know where to find him if u want to play with him.
U just reference this instance with the identifier 'exampleTwo'.
edit:i should make clear that only with primitive data types like,Number,String,Boolean does the actual value go in the bucket.
With all other data types,only a reference to the value goes into the bucket,so its like if your trying to store a 'House' object in an empty bucket....then that just wont work well will it?....so its easier & much quicker to just put a little note with an address(reference) to the 'House' object in the bucket,if u know what im driving at...
goodwinsvml
12-07-2011, 10:53 PM
I think a more accurate explanation is:
exampleone is the place where you intend to store a bucket if you ever get one.
exampletwo is an empty bucket.
exampleone is just a typed handle. It is a null pointer. There is no memory associated with it. You can't run any of the ArrayCollection methods against exampleone. The only thing you can do with it is assign an existing ArrayCollection to it. (Then you can use the methods.)
exampletwo is a handle to some memory (actually points to a memory location), but the memory contains no data. You can run all ArrayCollection methods against exampletwo even though it is empty.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.