Object question

BlitzMax Forums/BlitzMax Beginners Area/Object question

ok, perhaps a n00b question, but therefor I breathe. Why won't this code work? I expect to be able to put any instance of any type into an array, upon which I will be able to update each instance from within the Ta array. I intend to have update methods in each and every object I add to this list.

Type Ta
	Field objectarray:Object[]
	Field maxobjects:Int
	
	Method Add(e:Object)
		maxobjects:+1
		objectarray=objectarray[..maxobjects]
		objectarray[maxobjects-1]=e ' <- add e:Object to array
	End Method
	
	Method run()
		For Local t:Int=0 To maxobjects-1
			objectarray[t].update ' <- assuming this update is the update method from Tb
		Next
	End Method
End Type

Type Tb
	Global index:Int
	
	Method New()
		index:+1
	End Method
	
	Method update()
		Print "yay \o/ "+index
	End Method
End Type


Local a:Ta=New Ta

a.Add New Tb
a.Add New Tb
a.Add New Tb
a.Add New Tb

a.Run

End


That's because your array is full of type Object and not type Tb. Since .update is not a method of type Object then it won't work. You need to typecast the call like so:
Tb(objectarray[t]).update
If you wish to be able to use a multiple of different types, then you need to create a base type with abstract methods and extend that.
Type TBase
	Global index:Int
	Method New()
		index:+1
	End Method
	
	Method update() Abstract
End Type

Type Ta
	Field objectarray:TBase[]
	Field maxobjects:Int
	
	Method Add(e:TBase)
		maxobjects:+1
		objectarray=objectarray[..maxobjects]
		objectarray[maxobjects-1]=e ' <- add e:Object to array
	End Method
	
	Method run()
		For Local t:Int=0 To maxobjects-1
			objectarray[t].update ' <- assuming this update is the update method from Tb
		Next
	End Method
End Type

Type Tb Extends TBase
	Field Typeindex:Int
	
	Method New()
		Typeindex = index
	End Method
	
	Method update()
		Print "yay \o/ "+Typeindex
	End Method
End Type

Type Tc Extends TBase
	Field Typeindex:Int
	
	Method New()
		Typeindex = index
	End Method
	
	Method update()
		Print "yipeee!!!! "+Typeindex
	End Method
End Type


Local a:Ta=New Ta

a.Add New Tb
a.Add New Tb
a.Add New Tc
a.Add New Tc

a.Run

End


Object doesn't have an update method, if you want it to still be an array of Object use SendMessage which all Objects have, or cast to Tb.