Best way to reference a parent type

BlitzMax Forums/BlitzMax Programming/Best way to reference a parent type

Imphenzia(Posted 2009) [#1]
The code below gives me the error "Unhandled Exception:Attempt to access field or method of Null object" and I understand why the error occurs but I'm not sure how to fix it.

In this example, the idea is that a BaseType is used for common objects and it can have a parent object that it inherits x from. Then the ExtendedType should also have parent functionality and use the x field, but also have additional fields that act in the same way in the extended type, namely y in this case.



I gather that the line Return Self.parent.getY() + Self.y produces the error because the super type doesn't have the getY() method, but nor do I want it to.

If I do not have the Field parent:ExtendedType in the ExtendedType it instead produces the error "Compile Error Identifier 'getY' not found... which is also understandable.

Any ideas of how I'd go about to sort the above issue out? Many thanks in advance.


DavidDC(Posted 2009) [#2]

How's that? Doubling up on Field names in a type extension can be a recipe for pain.


Pete Rigz(Posted 2009) [#3]
I think you need to use casting like this:

Return ExtendedType(Self.getParent()).getY() + Self.y


I'd think carefully about your design though, what are you trying to achieve?


Tommo(Posted 2009) [#4]
You can override getParent method like this:

Type ExtendedType Extends BaseType
	'Field parent:ExtendedType  
	'This shouldn't be here, otherwise you have 2 different .parent fields (one for super, one for self)
	
	Field y:Double
	
	Method getY:Double()
		If Self.getParent() = Null Then
			Return Self.y
		Else
			Return Self.parent.getY() + Self.y
		End If
	End Method
	
	Method setY(y:Double)
		Self.y = y
	End Method

	'Override getParent method
 	Method getParent:ExtendedType()
		return ExtendedType(self.parent)
 	End Method
End Type



Muttley(Posted 2009) [#5]
@Tommo: You forgot to change the else clause to use getParent()

It's really down to personal preference, but I'd avoid directly accessing files where possible. It makes things a lot easier in the future if you decided to change things around if you're using accessors. I generally avoid using Self unless it's absolutely required and use _fieldNamesLikeThis to denote they should be treated as private and not accessed directly:




Tommo(Posted 2009) [#6]

You forgot to change the else clause to use getParent()



Ah... sorry for that.
You have got a typo too, "accessing fields". ;)

I personally like fast code (that's one advantage of bmax, I think), but I know it's not good for bigger projects.


Muttley(Posted 2009) [#7]
@Tommo: Hahaha, sod's law. :D


Imphenzia(Posted 2009) [#8]
Excellent - thank you all for your quick responses and solution to my proplem. Very impressive :)

As Pete mentioned I'm not sure whether I SHOULD be doing this or not. In my particular case I have a base object, e.g. "TObject" that has a position x,y but doesn't necessarily have an image assigned to it, but I want to be able to assign transitional effects to it to move from one location to another. My extended object is e.g. "TSprite Extends TObject" which then has alpha, scale values in addition to TObject and I want to use the same syntax for transition between different alpha values as I did for position.

Then the question is - should I lift the field variables of alpha, rotation, and scale to TObject instead even though they are not used by that object - or should I break them out into the subtype?

I'm open to suggestions or I can clarify more if needed.


TaskMaster(Posted 2009) [#9]
Wouldn't it just be easier to override the getx and gety methods on the extended type.

And if you want to add the parent x to the extended x you could call:

Base Type:

Method GetX:Int()
Return X
End Method

Extended Type

Method GetX:Int()
Return Super.X + X
End method

What I don't get is why you are using the Super.X at all???