Tlists and Inheritance

BlitzMax Forums/BlitzMax Programming/Tlists and Inheritance

_Skully(Posted 2009) [#1]
This works...

Type a
	Global as:TList=New TList
	Field b
	Method New()
		as.AddLast(Self)
	End Method
End Type

Type b Extends a
	Field c
End Type

Type c Extends a
	Field d
End Type

Local ba:b=New b
Local ca:c=New c

For Local ia:a=EachIn a.as
	If b(ia)
		Print "TypeB"
	Else
		Print "TypeC"
	End If
Next


but is there a way to do *something like*:

For Local ia:a=EachIn a.as
	Select ia
	Case b
		Print "TypeB"
	Case c
		Print "TypeC"
	End Select
Next


Without setting an objectType in type a?


Jesse(Posted 2009) [#2]
you can do it like this:
SuperStrict
Type Ta
	Global as:TList=New TList
	Field b:Int
	Method New()
		as.AddLast(Self)
	End Method
End Type

Type Tb Extends Ta
	Field c:Int
End Type

Type Tc Extends Ta
	Field d:Int
End Type

Local ba:Tb=New Tb
Local ca:Tc=New Tc
For Local ia:Ta=EachIn Ta.as
	Select True
	Case Tb(ia)<>Null
		Print "TypeB"
	Case Tc(ia)<>Null
		Print "TypeC"
	End Select
Next


why don't you do something like this instead:
Type a
	Global as:TList=New TList
	Field b
	Method New()
		as.AddLast(Self)
	End Method
	
	Method display() Abstract
	
End Type

Type b Extends a
	Field c
	Method display()
		Print "TypeB"
	End Method
End Type

Type c Extends a
	Field d
	
	Method display()
		Print "TypeC"
	End Method
End Type

Local ba:b=New b
Local ca:c=New c

For Local ia:a=EachIn a.as
	ia.display()
Next



_Skully(Posted 2009) [#3]
Ya, I just ended up adding

Field ID


to type a and for each extended object...

Method New()
   Self.ID=ID_Player ' for example
End Method



dmaz(Posted 2009) [#4]
yuck... what jesse suggested is cleaner and correct.

and to further encapsulate...
Type a
	Global as:TList=New TList
	Field b
	Method New()
		as.AddLast(Self)
	End Method
	
	Method display() Abstract
	
	Function displayAll()
		For Local ia:a=EachIn as
			ia.display
		Next
	End Function
	
End Type

Type b Extends a
	Field c
	Method display()
		Print "TypeB"
	End Method
End Type

Type c Extends a
	Field d
	
	Method display()
		Print "TypeC"
	End Method
End Type

Local ba:b=New b
Local ca:c=New c

a.displayAll