Subclass an Event?

I have a custom class based on a Canvas (call it CLASS_A)
its Paint event is very simple

reDraw g // to keep the "logic" away from the event itself

I then have another class which is a Subclass of CLASS_A (call it CLASS_B)

I need the PAINT event for CLASS)_B to pass an additional flag to “redraw”

reDraw g , true // redraw has this argument default to false 

but you can’t seem to override events

You need to do it the other way around. The subclass implements the event and then you do a raiseevent paint(g).

Confused… How can CLASS_B raise the event, when PAINT is an event that is not directly controllable (except via Invalidate)

CLASS_A has a PAINT event, but because of that CLASS_B does not

You can create event definitions with the same name as events you’ve implemented so that you can re-raise them.

This is legal:

CLASS_A Event Handlers Paint(g as Graphics, areas() as RealBasic.Rect) Event Definitions Paint(g as Graphics, areas() as RealBasic.Rect)

Sub Paint(g As Graphics, areas() As REALbasic.Rect) Handles Paint
  // Draw, draw, draw
  
  RaiseEvent Paint(g, areas)

End Sub

That I understand, and perhaps I’m just having a “Senior Moment” here.

CLASS_A is the master class, with all the base logic / graphics etc…
while I mentioned CLASS_B, there is also CLASS_C and CLASS_D all of which inherit 90% from CLASS_A

The app has instances of all of those classes (ie. CLASS_A is used by itself as well)

What it sounds like you are suggesting, is to create a CLASS_Z which is never instantiated but used as the superclass for A-D
then have this

CLASS_Z

Sub Paint(g As Graphics, areas() As REALbasic.Rect) Handles Paint
    RaiseEvent Paint(g, areas)
End Sub

CLASS_A thru CLASS_D

Sub Paint(g As Graphics, areas() As REALbasic.Rect) Handles Paint
   reDraw g,<class specific parameter>
End Sub

add all the parameters to the event definition, and set a default value in the parameter definition
that way, you will ignore the first level of class, and use the parameter in the sub-subclasses or co-classes. as you need.

[quote=421700:@Jean-Yves Pochez]add all the parameters to the event definition, and set a default value in the parameter definition
that way, you will ignore the first level of class, and use the parameter in the sub-subclasses or co-classes. as you need.[/quote]

Ok… this is still not clear.

WHO raises the Event vs WHO implements the REDRAW method?

In the subclass add an event definition “paint(g as graphics)”. In the paint event of the subclass you add a “raiseevent paint(g)” where you need it. Then the parent class also has a paint event. The subclass can do additional stuff and the paint event in the parent class isn’t changed when there is no subclass.

Thats my point… the subclasses (CLASS_B) do not have a Paint Event as it is consumed by the Superclass (Class_A)

sure I can add a EvenDef to CLASS_B, but that does no good, as nobody can call it.

Perhaps if one of you who seems to understand this could post a consise example, because I’m not wrapping my head around this

If I’m understanding correctly, this should do the trick.

CLASS_A

  Event Handlers
      Paint(g as Graphics, areas() as RealBasic.Rect)
  Event Definitions
      Paint(g as Graphics, areas() as RealBasic.Rect)
  Methods
      reDraw(g as Graphics, param as Variant)
Sub Paint(g As Graphics, areas() As REALbasic.Rect) Handles Paint
   reDraw g,<class specific parameter>  // Does your drawing
   RaiseEvent Paint(g, areas)           // Raises the event for CLASS_B which is a subclass of CLASS_A
End Sub

I have been having trouble following your design pattern since the thread where you’d asked about it, so I’m having trouble making suggestions that are useful to you. If this doesn’t work out for you, I’ll need to see what you’re actually doing to help.

If the subclass doesn’t have the event then remove it from the parent class. Do what we have tried to tell you, then you should get the event back.

You never have done this before???

The problem is that CLASS_B needs to

reDraw g,"XYZ"

While CLASS_A would be

reDRAW g,"ABC"

What you posted, CLASS_B would still do “ABC” and then “XYZ”

CLASS_B can’t (shouldn’t) call REDRAW twice… which based on what I understand … .is exactly what I am observing…

Generally what that means is you’ll need to return a value to determine if the behavior has been overridden.

[code]#tag Class
Protected Class Class_A
Inherits Canvas
#tag Event
Sub Paint(g As Graphics, areas() As REALbasic.Rect)
If Not Paint(G) Then
Self.Redraw(G, “ABC”)
End If
End Sub
#tag EndEvent

#tag Method, Flags = &h1
	Protected Sub Redraw(G As Graphics, Tag As String)
	  
	End Sub
#tag EndMethod

#tag Hook, Flags = &h0
	Event Paint(G As Graphics) As Boolean
#tag EndHook

End Class
#tag EndClass

#tag Class
Protected Class Class_B
Inherits Class_A
#tag Event
Function Paint(G As Graphics) As Boolean
Self.Redraw(G, “XYZ”)
Return True
End Function
#tag EndEvent
End Class
#tag EndClass
[/code]

this seems to work…

Sub Paint(g As Graphics, areas() As REALbasic.Rect) Handles Paint
  If InStr(Self.parent.name,"._wrapper_")=0 Then 
    debug "CLASS_A PAINT"
    reDraw g,"BTN"
  Else 
    RaiseEvent Paint(g, areas)
  End If
End Sub

but I think I like Thoms idea better

[quote=421719:@Dave S]this seems to work…

Sub Paint(g As Graphics, areas() As REALbasic.Rect) Handles Paint If InStr(Self.parent.name,"._wrapper_")=0 Then debug "CLASS_A PAINT" reDraw g,"BTN" Else RaiseEvent Paint(g, areas) End If End Sub [/quote]
There’s always multiple ways to solve a problem, but this seems clumsy since it relies on naming convention. Naming should be for organization, not for functionality.

I totally agree… your idea was much cleaner… and works perfectly

I do wish we had something like

If Not EventImplemented Paint Then DoSomeDefaultBehavior() End If