Overloading

No not your plate (despite it being right around American Thanksgiving)

Sometimes when you write your code its convenient to have several versions of “the same” method.

For instance, in Xojo you might look at the AddRow method on the Listbox class )in 2019r1.1) and see that it has several forms

ListBox.AddRow(ParamArray Item as String) 
ListBox.AddRow(items() as String) 
ListBox.AddRow() 

This is a great example of “overloading” – having one method with many different signatures. Each is unique and distinct from the others and the compiler doesn’t have to do conversions that might lead to ambiguous situations when you pass several strings to the method.

The same can be said for the classic Date class. It had several constructors like

Date.Constructor()
Date.Constructor(CopyDate as Date)
Date.Constructor(Year as Integer, Month as Integer = 1, Day as Integer = 1, hour as Integer = 0, minute as Integer = 0, second as Integer = 0)
Date.Constructor(Year as Integer, Month as Integer = 1, Day as Integer = 1, hour as Integer = 0, minute as Integer = 0, second as Integer = 0, GMTOffset as double)

Note the last two are VERY similar but vary in the very last parameter.

And you can also do this in your own classes & code.

You could easily add many constructors to your classes so you can create instances using a wide variety of parameters.

As long as each overload can unambiguously be called the compiler wont have any trouble with things.

Its usually the “unambiguous” part that gets people into trouble as they defer to using default values for parameters. For example, if I have the following class along with the constructors shown you may find you have issues – but only sometimes

Class TestClass
   Public Sub Constructor()
   End Sub
   Public Sub Constructor(foo as integer = 0)
   End Sub
End Class

If later I have code like

Dim c As New TestClass(1)

This will work without issue since TestClass only has one possible constructor that matches that signature. So things compile & run fine.

However if later I try to do

Dim c As New TestClass

I will now get a compile error saying

Window1.Open, line 1
There is more than one item with this name and it’s not clear to which this refers.
Dim c As New TestClass

The compiler cannot figure out whether I mean to call the constructor with no parameters, or the one that has a single parameter with a default value that I simply have left out. And so it gives up trying and you get a compile error.

Once you realize that the issue is that the compiler doesn’t know which method you meant you can address the problem. Perhaps in this case a constructor with a default value for the parameter makes no sense and so you’d remove that default value. Or you decide that you really only need the Constructor with the single parameter and default value since you can deal with it as if it were the no parameter version.

Often when I’ve encountered this problem it is a symptom of having optional or default values that, when omitted, make one method have the same signature as the other.

Careful out there and try not to confuse the compiler too much.