Extending TImage

BlitzMax Forums/BlitzMax Programming/Extending TImage

Hello,
could someone please tell me why TImage always return Null, if i try to cast it:
Type TMyImage Extends TImage

        'result = Null
	Function Open:TMyImage (url:Object)
		Local image:TMyImage = TMyImage(TImage.Load(url,MASKEDIMAGE,255,0,255))
		Return image
	End Function

        'result = null
        Function Open2:TMyImage (url:Object)
                Local image:TImage = TImage.Load(url,MASKEDIMAGE,255,0,255)
                If image Return TMyImage(image)
         End Function

End Type


I've tested several ways, but casting TImage doesn't seem to work.

If you check your class you will find out that TMyImage actually isn't handled as a fully TImage extend.

Add a function create for example that returns :TMyImage
On a correct extend, the compiler would already throw an error that function declaration differs (as TImage.Create returns :TImage) but it does not in that case.

Basically in TImage.Load you are creating a TImage, but not a TMyImage. So since image is really a TImage casting it to a TMyImage will return null. Its the old saying of how a square is a rectangle, but a rectangle is not a square.

TMyImage(TImage.Load(url,MASKEDIMAGE,255,0,255))
TImage.Load returns a TImage, so casting to TMyImage will return Null.

You could write an image wrapper:
Type TMyImage Extends TImage
  Field src:TImage

  Method DoThisForAllMethods ( Parameters )
    Return src.DoThisForAllMethods ( Parameters )
  EndMethod
EndType