Tuesday, December 27, 2005

Method "Overload" Example: A Debugging Aid

Don't let the word "overload" bother you. We won't be "overdoing" anything. In RB, "overload" means giving more than one method (i.e., Function or Sub) the same name, but with a different number or different types of passed parameters. [Originally I had said "(or different return type)," but that was wrong - see the helpful comment by Will Leshner smong the comments to this blog entry.] Many languages do not allow you to do that, but REALbasic does.

Let me give as an example something I use regularly as a debugging aid. Now, many people use MsgBox to help in debugging by displaying the value of a particular variable at a certain point in the Project. For example, suppose you want to check on the value of the integer XMLLineNumber. You can insert the following at the desired place in your program:
MsgBox "XMLLineNumber" + EndOfLine + Str(XMLLineNumber)
(The "EndOfLine" is there to put the value on a new line in the MsgBox.)

Or suppose you want to check on the value of the string XMLLine. You can insert the following:
MsgBox "XMLLine" + EndOfLine + "|" + XMLLine + "|"
(The "|" is used so that you can see if there is any blank space at the beginning or end of XMLLine.)

Or suppose you want to check on the value of the Boolean IsSource. You can do that with this:
If IsSource Then MsgBox "IsSource" + EndOfLine + "True" 
Else MsgBox "IsSource" + EndOfLine + "False"
It's a headache, however, to have to use three different formats (integer, string, and Boolean) and remember which one to use for which situation.

Instead of doing that, let's define (and "overload") a Function called S, all forms of which return a String (my reason for choosing to call it "S"), but which respond to different types of passed parameters.

First, define S with an integer Param1 as a passed parameter in this way:
Function S (Param1 As Integer) As String
Return EndOfLine + Str(Param1) + EndOfLine
End Function
Second, define S with a string Param1 as a passed parameter in this way:
Function S (Param1 As String) As String
Return EndOfLine + "|" + Param1 + "|" + EndOfLine
End Function
Third, define S with a Boolean Param1 as a passed parameter in this way:
Function S (Param1 As Boolean) As String
If Param1 Then Return "True" Else Return "False"
End Function
Once this is done, you can just use S, regardless of which of the three types of passed parameter we have in mind. Example:
MsgBox "XMLLineNumber" + S(XMLLineNumber)

MsgBox "XMLLine" + S(XMLLine)

MsgBox "IsSource + S(IsSource)
or even
MsgBox "XMLLineNumber" + S(XMLLineNumber) + "XMLLine" 
+ S(XMLLine) + "IsSource" + S(IsSource)
Try it -- you may like it!

Barry Traver



Blog Home Page: http://traverrb.blogspot.com/

Programs and Files Discussed in the Blog: http://traver.org/traverrb/

6 Comments:

At 9:42 PM, Blogger silverpie said...

Two improvements I would suggest: CStr() instead of Str(), so that if the user's format isn't US, it will still display as the user expects. Second, have the numeric version take a double as its parameter--it will still accept integers, but can thus show fractional values too.

 
At 11:17 PM, Blogger ArbourGlenTech said...

Hey Barry,

I just wanted to comment that as a RealBasic newbie, I really appreciate what you are doing here. Keep up the good work!

Darry|

 
At 8:20 AM, Blogger Barry Traver said...

arbourglentech,

I regard myself as still a REALbasic newbie, so I appreciate your words of encouragement very much.

I don't have access to your email address (or, if I do, I don't know how yet to access it), but mine is rb@ix.netcom.com. Feel free whenever you like to send me a private email (and I hope that you will continue to post comments, questions, etc. from time to time).

Warm regards,

Barry

 
At 8:31 AM, Blogger Barry Traver said...

silverpie,

Great comment! I had forgotten about CStr() The existence of that function makes unnecessary the specific example I provided of a useful Method "overload," but my example still provides, I hope, a good illustration of how the "overload" process works.

Also, I hadn't realized (but probably should have) that a double will also accept integers, so you taught me something new (and useful).

Thanks for your post. I hope that you will feel free to post future comments, suggeted improvements, constructive criticism, etc.

Warm regards,

Barry

 
At 11:39 AM, Blogger Will Leshner said...

A small nit: you can't really overload a method with another method that differs only by return type. Their parameters must differ. You could have two methods that differ in both parameters and return type, but I'm not sure that's really overloading.

You can override a method in a super class with a method that has the subclass as a return type (where the super class has the super class as a return type). That's return covariance.

 
At 6:10 PM, Blogger Barry Traver said...

Will,

You're absolutely right! I don't know why I said what I said. I just now checked the RB docs and Neuberg's book, and neither provides any support for my parenthetical comment, so I'll change the blog entry accordingly.

Thanks for taking the time to post the comment!

Barry

 

Post a Comment

<< Home