Superclass Properties

So how are you supposed to get a Superclass Property (when there is a subclass property with the same name, but different value)?

in subclass

y=super.x

doesn’t work (per the LR)…
but returns a SUPER (pun) error message

You can’t use computed properties either
the only way is to define a METHOD in the superclass to replicate what a computed property does.

You can shadow the property: in the code of the subclass’ property you need to cast Self to the superclass type instead of using the Super keyword. But be careful, if you address the subclass as its superclass, it will call the superclass’ implementation, not the subclass.

Class MyCanvas Inherits Canvas Property Enabled As Boolean Get Return Canvas(Self).Enabled End Get Set Canvas(Self).Enabled = value End Set End Property End Class

Dim c1 As MyCanvas c1.Enabled = True // will call the above property setter

Be careful with this case:

For i As Integer = 0 To Window.ControlCount - 1 If Window.Control(i) IsA Canvas Then Dim cnv As Canvas = Canvas(Window.Control(i)) cnv.Enabled = True // will NOT call the above property setter End Next

You need to first check for the subclass case:

For i As Integer = 0 To Window.ControlCount - 1 If Window.Control(i) IsA MyCanvas Then Dim cnv As MyCanvas = MyCanvas(Window.Control(i)) cnv.Enabled = True // will call the above property setter ElseIf Window.Control(i) IsA Canvas Then // all other canvases Dim cnv As Canvas = Canvas(Window.Control(i)) cnv.Enabled = True End Next

Actually the question was more … Why does this restriction even exist?
Xojo is the only place where I have seen the inability to refrence the properties of a Superclass, and only its methodes

I think it is faster because the methods (the getters and setters) are not virtual functions, meaning there is no lookup along the class hierarchy necessary.

That I can understand… to a point.
“super” is not a straight forward declaration… since you can have a class that is sub of a sub of a sub…
but I would think that running that chain to find a property would be identical to looking for a method

  • does my super have method or class
  • Yes… great… am done looking
  • No… go up a level in the chain
  • have I passed the end of the chain - if so, ERROR
  • otherwise repeat this loop

Maybe it comes from variable shadowing:

[code]Class A
Property x As Integer
End Class

Class B Inherits A
Property x As Integer // this shadows the x of A, you need to cast x to A to access it
End Class[/code]
… and when computed properties were introduced they were introduced with the same scope, so code would not break if somebody replaced x with a computed property x.

Personally I don’t like computed properties. Regular methods would be better because one could “catch” the setter in a proper way. For example it is impossible to catch Enabled being set for a subclass of a built-in control without shadowing.

Last idea: maybe it is related to the window editor and its inspector?