How to override a create function?

BlitzMax Forums/BlitzMax Beginners Area/How to override a create function?

Arowx(Posted 2005) [#1]
Hi Gents I'm getting Null pointer exceptions when attempting to override a parents Create method e.g.

Type testA
	Field x:Int
	Field y:Float
	Function Create:testA(newx:Int)
		newA:testA = New testA
		newA.x = newX
		Return newA
	End Function
End Type

Type testB Extends testA
	Function Create:testB(newx:Int)
		Return testB(Super.Create(newx))
	End Function
End Type
'Test Harness

Graphics 640, 480, 0

Global test:testB
test = testB.Create(10)
test.y = 5

While Not KeyHit(KEY_ESCAPE)
	Cls
	Flip
	FlushMem
Wend

This generates an unhandled exception on this line :
test.y = 5
What I'm trying to do is ensure the old version of create is called by default in the new version so I can then add additional elements. Unfortunatly I'm doing something fundamentally wrong?!

Can you help?

Regards

Merx


Arowx(Posted 2005) [#2]
Totally wierd I've just tried the test code in protean and received the Null field error and then tried again in the BlitzMax IDE only to get no error?

Stranger is that the BlitzMax IDE allows my application version of this to work with IGlass, I'm attempting to extend an IGlass window into a tilemap selection window, however it only works when I don't flush the memory!

This is wierd!!!


skidracer(Posted 2005) [#3]
I would check that debug mode is on in BlitzMax IDE.

You can't override functions, when you call
Return testB(Super.Create(newx)

BlitzMax is creating a TestA type then casting to the type TestB which as you can only cast successfully to a base class is returning null.

The following is an example of using Create methods instead of functions which allows you a little more flexibility. The trick is to remember to Return Self from all your Create Methods and use the myblob=new blob.Create() style creation:

Type A
	Method Create:A()
		Return Self
	End Method
End Type

Type B Extends A
	Method Create:B()
		super.Create()
		Return Self
	End Method
End Type

MyB:B=New B.Create()



kyoryu(Posted 2005) [#4]
Is there some reason to not just use the New method instead, if you're not passing parameters?


Bot Builder(Posted 2005) [#5]
Good point - Iīd overide the new method instead if you arenīt using parameters.


Arowx(Posted 2005) [#6]
Hi Gent's thanks for the help! Using the Self method cracked it, the problem was that the Create Function in the GUI library is being used to create a new object and populate it and I needed to extend/wrap this process.

In the end I needed to a new create method with a different name as a wrapper to the Create function.

Cheers

Merx