just a simple list with objects ? how

Monkey Forums/Monkey Programming/just a simple list with objects ? how

GC-Martijn(Posted 2011) [#1]
H! everyone,

I downloaded the demo, but that gives me not enough help to make a simple list with objects.
So now I have the full version with forum support ;)

What I want to do is make a
Field something:List<Object> or something:Stack<Object> yust something that hold my created objects.
inside a other class.

This don't work:

Strict
Import mojo

Class A
	Field assets:Stack<B> ' a list with B objects
	
	Method add2BList : Void()
		Self.assets.Push(New B())
	End
End

Class B Extends A
	Field name:String
End


Function Main:Void()
	Local helloA:A
	
	helloA = New A()
	helloA.add2BList()
End



Complie HTML5 > TypeError: Cannot call method 'bbm_Push' of null

I think that New B() return null, and that's why I get that error.

What is the best way to create a [holder] with objects inside a other class ?

Thanks,
GCMartijn


Raz(Posted 2011) [#2]
In Class A add

Method New()
assets = New Stack<B>
End



Raz(Posted 2011) [#3]
The error suggests that the assets object while defined as a stack of B's hasn't been set to one.


GC-Martijn(Posted 2011) [#4]
thanks raz, I see it now.

Or I could do this
Field assets:Stack<B> = New Stack<B>


Is there a simple explanation what to use Stack/List or something else to hold objects ?

I think i'm going to use List<B> and use AddLast, but I don't know why haha


Raz(Posted 2011) [#5]
Yeah you can do that too :)

Lists are fine, but it depends on your target and how you intend to use them. I can't speak for everyone else, but when developing for Xbox via XNA, you have to initialize all objects to start with (for example, I have 1000 particle objects) and activate/deactivate them as required. XNA really does not like the creation of objects during play (e.g. dog = New Dog(100,200))


Samah(Posted 2011) [#6]
XNA really does not like the creation of objects during play (e.g. dog = New Dog(100,200))

I'm sure Android is even worse. :)

I recommend you take a look at Diddy's ArrayList class. There's a wiki page on how to use it.
http://code.google.com/p/diddy/wiki/ArrayList


GC-Martijn(Posted 2011) [#7]
I will have a look at that thanks.