acces a field in extended type

BlitzMax Forums/BlitzMax Beginners Area/acces a field in extended type

Abomination(Posted 2006) [#1]
I've read this tutorial:
http://www.alsbonsai.com/john/BlitzMax_OOP_Tutorial.pdf
this is one of the examples:
Type TEntity
Field x:Float, y:Float
Method Draw() Abstract
End Type
Type TPlayer Extends TEntity
Field Health:Int
Method Draw()
SetColor 0, 0, 255
DrawOval x, y, 5, 5
End Method
End Type
Type TRobot Extends TEntity
Field Health:Int
Method Draw()
SetColor 255, 0, 0
DrawOval x, y, 5, 5
End Method
End Type
Type TBuilding Extends TEntity
Field Enterable:Int
Method Draw()
SetColor 255, 255, 255
DrawRect x - 5, y - 5, 10, 10
End Method
End Type
Graphics 640, 480
Local EntityList:TList = New TList
Local obj:TPlayer = New TPlayer
obj.x = 5
obj.y = 7
EntityList.AddLast(obj)
Local obj2:TBuilding = New TBuilding
obj2.x = 15
obj2.y = 3
EntityList.AddLast(obj2)
'Draw all entities
Local ent:TEntity
For ent = EachIn EntityList
ent.Draw()
Next
Flip
WaitKey

But I don't understand how I can access the field "enterable" in the buildingtype, while going through the list.
A bit of help please?


Dreamora(Posted 2006) [#2]
You have to cast to type TBuilding and access it on that one (if it is <> null). You can't access it on its base type.


Abomination(Posted 2006) [#3]
hmm... I actualy knew that, but thanks anyway, because it didn't come to mind.
"for those who don't know": TBuilding(ent).enterable


Defoc8(Posted 2006) [#4]
might be safer to assign first!

tmp:TBuilding=TBuilding(ent)
if(not tmp=null)tmp.enterable=true

or

if(not Tbuilding(ent)=null)Tbuilding(ent).enterable=true

otherwise you may endup accessing a field of null object.
[as dreamora suggested]

you will probably also want to output some debug text +
halt the program if you cannot safely ignore it..

Im not sure about the implications of using
the "T" letter for your class names..
TList, TStack, TDriver
Wouldnt this potentially cause problems unless accessed
via a custom mod + fully qualified..

mystuff.mymod.TList

id personally avoid using the exact naming convention
used by BRL..avoid potential clashes with official modules.

perhaps my reasoning is flawed.. ;)