Object question

BlitzMax Forums/BlitzMax Beginners Area/Object question

CS_TBL(Posted 2007) [#1]
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



TomToad(Posted 2007) [#2]
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.



Azathoth(Posted 2007) [#3]
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.