Add New Last/First

BlitzMax Forums/BlitzMax Beginners Area/Add New Last/First

pc_tek(Posted 2011) [#1]
Hi all!

How do I modify the code (shown) to allow both the adding of first AND last depending on a variable. I have used the code succesfully but now have a need to add first.

Type TObject
	Global all:TList = New TList
	Field link:TLink
	
	Field ident:Byte
	Field xpos:Float,ypos:Float	
	Field fram:Byte
	Field dead:Byte,squished:Byte
	Field minfram:Byte,maxfram:Byte
	
	
	Method New()
		link = all.AddLast(Self)
	End Method
	
	Method remove()
		link.Remove()
		link = Null 'for safety
	End Method
	
	Function Create:TObject(i,x,y)
		Local anim:TObject = New TObject
		anim.ident=i
		anim.xpos=x
		anim.ypos=y
		Return anim
	End Function
End Type

For Local i = 0 To 10
	TObject.Create(0,0,0)
Next

For Local anim:TObject = EachIn TObject.all
	anim.Remove()
	debuglog "HHH"
Next

For anim:TObject = EachIn TObject.all
	DebugLog "HERE"
Next



col(Posted 2011) [#2]
Hiya,

I'm taking an educated guess to what you mean with 'to allow both the adding of first AND last depending on a variable.' so I'm assuming this:-

First you'd remove the New() method as this is a default action that you that don't want?
Then in the Create(...) method add an extra variable to allow you to choose whether the new instance is to be added to the beginning or end of the TList.

Strict

Type TObject
	Global all:TList = New TList
	Field link:TLink
	
	Field ident:Byte
	Field xpos:Float,ypos:Float	
	Field fram:Byte
	Field dead:Byte,squished:Byte
	Field minfram:Byte,maxfram:Byte
	
	'Method New()
	'	link = all.AddLast(Self)
	'End Method
	
	Method remove()
		link.Remove()
		link = Null 'for safety
	End Method
	
	Function Create:TObject(i,x,y,First) '<-------Added extra variable to allow First or Last position entry
		Local anim:TObject = New TObject
		anim.ident=i
		anim.xpos=x
		anim.ypos=y
		
		If First
			anim.link = all.AddFirst(anim)
		Else
			anim.link = all.AddLast(anim)
		EndIf
		
		Return anim '<- Not sure why you want to Return this instance, not needed in this example.
	End Function
End Type

For Local i = 0 To 10
	TObject.Create(0,0,0,False) 'Use True to insert First
Next

For Local anim:TObject = EachIn TObject.all
	anim.Remove()
	DebugLog "HHH"
Next

For Local anim:TObject = EachIn TObject.all
	DebugLog "HERE"
Next


Last edited 2011


pc_tek(Posted 2011) [#3]
Many thanks Col