Using Self and Super for Fields

BlitzMax Forums/BlitzMax Programming/Using Self and Super for Fields

DirtBikeDude(Posted 2008) [#1]
Is it possible to use self and super for fields within extended types?


tonyg(Posted 2008) [#2]
What happens when you try?


DirtBikeDude(Posted 2008) [#3]
Type Base
	Field Test:String = "Base Field"
End Type

Type Extended Extends Base
	Field Test:String = "Extended Field"
	
	Method Debug()
		Print Super.Test
		Print Self.Test
	End Method
End Type

Local Example:Extended = New Extended
Example.Debug()


Gives an error "Identifier Test Not Found" at "Print Super.Test".
I guess that's my answer.


Grey Alien(Posted 2008) [#4]
You need to typecast it:

Print Base(Super).Test



Yan(Posted 2008) [#5]
Which makes the use of Super pretty redundant as you might just as well use...
Print Base(Self).Test


Super *should* work with fields, should it not?


Grey Alien(Posted 2008) [#6]
Yeah I guess it should unless I'm missing something (like it would break inheritance or something). It works with methods and functions of course:

Type Base
	Field Test:String = "Base Field"
	
	Method GetTest$()
		Return test
	End Method

	Function Test2$()
		Return "Base Test2"
	End Function
End Type

Type Extended Extends Base
	Field Test:String = "Extended Field"
	
	Method GetTest$()
		Return test
	End Method

	Function Test2$()
		Return "Extended Test2"
	End Function

	Method Debug()
		Print Super.GetTest()
		Print Self.GetTest()
		Print Super.Test2()
		Print Self.Test2()
	End Method
End Type

Local Example:Extended = New Extended
Example.Debug()