Class With Grouped methods?

Hi. I have created a custom canvas class and was wondering whether there was a way to group some of my related methods into a module like structure within the class? So for instance, say I have a bunch of methods all related to MouseEvents, is there a way to create a module like structure called “MouseEvents” and place it in the “CustomCanvas” class and then call one of the events like this:

CustomCanvas.MouseEvents.HandleCustomAction(X, Y)

This would a) make the code structure easier to navigate and b) make it easier to port those specific group of functions to another class etc.

I do it by naming the methods with a common prefix: mouseDown, mouseUp, mouseMove …
that way all related methods of the class are grouped.
you cannot insert a folder but at the top level of the project.

Thanks, Jean-Yves. This is what I am currently doing. I just wondered if there was another way as described.

I believe there’s a feature request for this, grouping methods and properties in folders.

Good to know. Thanks.

If you’re trying to keep your class a little cleaner by semi-hiding lesser used methods, you could add them to a module and have them extend the custom class.

E.g. in a “CustomCanvas” folder add a “MouseEvents” module, then add your “HandleCustomAction” method to the new module utilising the “Extends” keyword…

Public Sub HandleCustomAction(Extends cc As CustomCanvas, X As Integer, Y As Integer) MsgBox "Yay!" End Sub

Then your CustomCanvas subclasses can use me.HandleCustomAction(X, Y).

Doesn’t give you the CustomCanvas.MouseEvents.HandleCustomAction(X, Y) that you wanted, but does get the method out of the main class.

You can of course add other modules to the “CustomCanvas” folder to cover other groups of extension methods to the CustomCanvas, and add the CustomCanvas itself to the folder too to keep everything nice and neat.

If you have methods that you’d like to use on all kinds of subclasses, you can use the Extends keyword with class names higher up the hierarchy, such as RectControl or even Object.

That’s a great idea, Ian. Thank you. I guess the only issue with that is that within the module that extends the class how do you refer to the class control? By that I mean that if the method is within the actual class you would say “ = 1” and “self.mymethod(value)” etc. But you can’t use “self” and you can’t use “cc” since it’s an extend and the CustomCanvas reference is not passed to it as a parameter.

You simply pass the control to the module methods, or add a property to the module that points to the control.

I often use a folder to group a custom class and its module, sometimes objects and pictures.

You can use cc (or whatever you want to name it), it is passed.

Public Sub HandleCustomAction(Extends cc As CustomCanvas, X As Integer, Y As Integer) MsgBox cc.Name End Sub

By the way, this is totally not recommended as it’ll no doubt break stuff down the line, but because “Me” doesn’t mean anything in a Module, you can use it as the name of of the Extends param…

Public Sub HandleCustomAction(Extends Me As CustomCanvas, X As Integer, Y As Integer) MsgBox Me.Name End Sub

Pretty cool, but again, probably frowned upon by people that know better how the framework hangs together!

Thanks, Ian. Now I understand better. Cool idea :slight_smile: