How do I overload a method?

Maybe I don’t understand overloading as well as I thought I did – is this the proper use of it (and how do I do it with Xojo).

I have two different classes defined, call them Field_type and Calculation_type.
I have a method call Add_to_report.

Sometimes I want to call Add_to_report(field_type) and sometimes I want to call Add_to_report(calculation_type). Is there a way to create the Add_to_report method such that it can accept either class and process them appropriate?

Think of Method overloading as having a method that can accept different types.

For instance:

Sub Print (image as Picture, width as Integer, Heigh as Integer)
End Sub
Sub Print(sometext as String)
End Sub

are valid methods with different types and argument counts. No need to AddText(someText) and AddPicture( pic, picture width, picture height) in your class. Just Print, much easier to remember, better readability, easier to mantain.

In you case Add_to_Report( i as Integer) and Add_To_Report ( mydebuggmessage as string ) will make use of method overloading and you can benefit this nice OOP feature for you and free :wink:

Hope it helps!

[quote=211608:@C L Hinkle]Maybe I don’t understand overloading as well as I thought I did – is this the proper use of it (and how do I do it with Xojo).

I have two different classes defined, call them Field_type and Calculation_type.
I have a method call Add_to_report.

Sometimes I want to call Add_to_report(field_type) and sometimes I want to call Add_to_report(calculation_type). Is there a way to create the Add_to_report method such that it can accept either class and process them appropriate?[/quote]

Alternately

  1. put the method ON the class itself so Field_Type has an Add_To_Report method and Calculation_type has an Add_to_report method
  2. change the method on each so it takes a “report” as the parameter
  3. now you dont care and the instances just do the right thing when told to add themselves to a report

Ultimately it may be useful to define an interface and make each of these classes implement that interface
Then anything else you write that also needs to add itself to a report implements that interface and your off and every class can have a unique “add to report” implementation

In either case, no, overloading does not give you one method that can take either class. Overloading means you create 2 methods and give them the same name but different parameter types. You can use a single method that takes a Variant, and then based on the type passed to it process them as appropriate. But that is not overloading (and also not necessarily recommended).

Thanks, guys!
I see now that I had the wrong impression of how to use overloading. Tim’s describing exactly what I was attempting to do. But I’ve got another route I can take. I appreciate the help.
Chuck