Using the same method for two or more classes

This is a “theory” question, so no need to ask “What do you want to accomplish”. Just learning a bit!

So, say I have a method that swtiches the width and height properties. I can easily make this for either a listbox or for a canvas, using the relavent parameter “lb as listbox” or “can as canvas”.

But can I do something like “ct as control” and have the method accept either the listbox or canvas? I’ve been trying to figure it out, but haven’t been able to.

Related, can I do the same thing but with one or more classes that share a super? like setting a canvas parameter and having the method accept “mysuperdupercanvasclass” as well as a regular canvas?

[code]Sub myFunction(ctrl as RectControl)

if ctrl isa Listbox then
// do one thing
elseif ctrl isa Canvas then
// do another thing
elseif ctrl isa mysuperdupercanvasclass then
// do something else
end if

end sub[/code]

Great, thanks!

Variables (including method parameters) that are declared as some class will accept that class and all its subclasses.

Michel’s example will work just fine, but if they will access the properties present in all RectControls, the “if” is not needed. In fact, if you are accessing properties that are only present in a particular class, you should overload the method for each class.

Put an extension method into a module:

Sub Switch(Extends rc As RectControl) ... End
Note the following:
The inheritance three is Object –> Control –> RectControl –> Canvas or Listbox or …
If the property is in RectControl like for example Width, you can use rc.Width in above method.
If the property is in Listbox like for example DefaultRowHeight, you need to cast to Listbox:

Sub Switch(Extends rc As RectControl) If rc IsA ListBox Then Dim lbx As ListBox = ListBox(rc) lbx.DefaultRowHeight = 22 Else ... End End

Technically, that would be several methods. I tried to stick to the question.