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/

3 Comments:

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 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