Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sun Sep 15, 2019 7:30 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: MsgBox and Debugging
PostPosted: Fri Mar 19, 2010 6:04 am 
Offline

Joined: Fri Sep 30, 2005 1:53 pm
Posts: 914
Location: Philadelphia, PA
First, let me say that "MsgBox" is a BAD WORD in debugging, according to very knowledgeable people, such as Aaron Ballman as expressed in his recent book Ramblings on REALbasic (Aaron Ballman, 2009):

Quote:
MsgBox is not a Good Debugging Tool.... It may be a tempting debugging tool, but you should consider System.DebugLog instead. [See pages 22-24].

If you never use MsgBox in your debugging, feel free to skip the rest of this note. If (like me) at times you do give in to the temptation to use MsgBox as a debugging tool, you may want to continue reading. You may find it interesting, even if you do not decide actually to use it.

Second, "Overload" is NOT always a BAD WORD. In case you never came across the term before, it simply means (as I understand it) that you have more than one definition of the same word or method, although those different definitions usually involve different parameters.

Now, if you do use MsgBox, I wouldn't be surprised it you occasionally do things like this:

Dim I As Integer
I = Ubound (SomeArray)
MsgBox I

REALbasic doesn't like that last line, because -- although it knows what to do if there's a string after MsgBox -- it doesn't know what to do if there's an integer after MsgBox. So what you get is a syntax error message ("Parameters are not compatible with this function [MsgBox].)".

In order to use MsgBox, you ordinarily have to alter the last line to read like this:

MsgBox Str(I)

And that ordinarily has to be the approach whenever you want to use MsgBox with an integer.

So let's "Overload" the definition of "MsgBox." That is, let's teach REALbasic what to do -- not only when you have MsgBox followed by a string (it already knows that) -- but also when you have MsgBox followed by an integer.

In the RB code editor, choose "Add Method." The "Method Name" is "MsgBox", the "Parameters" entry is simply "A As Integer" (it already knows how to handle a string), the "Return Type" is left blank, and there's simply one line of code: "MsgBox Str(A)".

When you've done this, MsgBox knows how to handle not only simple things like this:

MsgBox I

but more complicated things like this:

MsgBox UBound (SomeArray) - 1

(You can, of course, teach your RB Project how to use MsgBox followed by a "double" (decimal) number, using the same technique.)

Enjoy!

Barry Traver


Top
 Profile  
Reply with quote  
 Post subject: Re: MsgBox and Debugging
PostPosted: Fri Mar 19, 2010 6:55 pm 
Offline

Joined: Thu Oct 05, 2006 11:45 pm
Posts: 1002
Location: Sol:Planet Earth:North America:California
Thanks for the tip, I hadn't thought of that, I always use str().

Personally, I think it is perfectly fine, during development, to use MsgBox. It can be very helpful in a pinch. I don't want to have to look at the console or system logs to see my debug messages, nor do I want to clutter them up with information I don't care about once I have dismissed the msgbox.

Sometimes though instead of msgbox I will use a floating window with nothing in it but a editfield and log things to that. But it sure is easy to use a MsgBox...

-Paul

_________________
This message made with 100% recycled pixels.
MacBook CD2.0/2GB/OSX 10.6.8 / REAL Studio 2010R1 Pro


Top
 Profile  
Reply with quote  
 Post subject: Re: MsgBox and Debugging
PostPosted: Sat Mar 20, 2010 4:34 am 
Offline

Joined: Fri Sep 30, 2005 1:53 pm
Posts: 914
Location: Philadelphia, PA
Paul,

Those are my feelings exactly. In fact, I use MsgBox extensively enough that I've also incorporated three MsgBox routines into my CodeHelper program, which can modify REALbasic Projects by modifying Project files in XML format.

Two routines are rather commonplace: one comments out all MsgBox statements in a Project and the other removes all the comment symbols in front of MsgBox statements. (I've thought about adding a routine to actually remove all MsgBox lines, since I think that it's a good thing NOT to have any MsgBox statements in the final version of a program, but I usually don't get to the stage of the "final version" of a program, just one that works but often looks awful).

The third routine may sound strange, but I've found it useful: it puts a MsgBox at the beginning of each Method telling the location of the Method and it comments out all of the code in the following lines of the Method. I've found this useful when I've somehow messed a Project up so bad that I would otherwise have had to start the Project again entirely from scratch.

The (easily removed) MsgBox at the beginning of Functions and Subs and the (also easily removed) comment symbols in front of the code let me fairly easily add the code back in a section at a time, piece by piece, by removing the MsgBox and uncommenting all of the code in that Method with a couple mouse-strokes (using Edit --> Uncomment in the RB IDE after selecting the Method text).

The process is actually much less confusing than it may sound, and CodeHelper automates the process so that the MsgBox and comment symbols can be added in a few seconds, even for Projects whose source code may take 180 pages to print out
(the situation with CodeHelper which itself performs over 100 tasks and includes close to 250 Functions and Subs). (This routine, of course, is useful in much smaller Projects as well.)

In short, I think MsgBox has gotten a bad rap, when in actually there are things that MsgBox can do that are not easily done otherwise (or sometimes cannot not be done at all).

Barry Traver


Top
 Profile  
Reply with quote  
 Post subject: Re: MsgBox and Debugging
PostPosted: Sat Mar 20, 2010 12:50 pm 
Offline

Joined: Thu Oct 05, 2006 11:45 pm
Posts: 1002
Location: Sol:Planet Earth:North America:California
Barry,

Cool thanks for the explanation and further good ideas. I feel the same way about MsgBox in a final app, not the best way to deliver information. I usually create a function that uses the Message Dialog class to do proper message dialogs instead of MsgBox, in the final app.

In the end, though, I don't understand the negative comments about using MsgBox as debugging as I haven't come across anything that has bitten me when I use them. As long as you remember to remove them, which it sounds like you have that down. :-)

Regards,

-Paul

_________________
This message made with 100% recycled pixels.
MacBook CD2.0/2GB/OSX 10.6.8 / REAL Studio 2010R1 Pro


Top
 Profile  
Reply with quote  
 Post subject: Re: MsgBox and Debugging
PostPosted: Sat Mar 20, 2010 2:16 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
One big problem with msgbox is that it changes the flow of the program you are debugging. It introduces all kinds of focus changes so you (potentially) get code firing that normally wouldn't. This also introduces the possibility of creating an endless loop where focus code pops up a msgbox which causes focus events to fire which pop up a msgbox, etc. If you haven't hit those issues, then msgbox is fine for your purposes, but be aware they exist.

My main issue with msgbox is that it gives you a very limited view of what is going on. You have to anticipate the information you're going to need about the program. Using a breakpoint, on the other hand, allows you to examine the entire state of the program, including the complete calling hierarchy. You can walk up the call stack and examine every method along the way.

Granted, using the debugger incurs similar focus issues as msgbox, that's why you have system.debuglog. It has the same limitations as msgbox, but it doesn't affect the flow of your program.

Just my 2 cents.


Top
 Profile  
Reply with quote  
 Post subject: Re: MsgBox and Debugging
PostPosted: Sat Mar 20, 2010 9:38 pm 
Offline

Joined: Thu Oct 05, 2006 11:45 pm
Posts: 1002
Location: Sol:Planet Earth:North America:California
Good points Tim. Actually I have run into those endless loops with MsgBox before. hehe It taught me not to put my msgbox in the windows activate event! :lol: Sometimes learning the hard way really does leave an impact. ;-)

_________________
This message made with 100% recycled pixels.
MacBook CD2.0/2GB/OSX 10.6.8 / REAL Studio 2010R1 Pro


Top
 Profile  
Reply with quote  
 Post subject: Re: MsgBox and Debugging
PostPosted: Sun Mar 21, 2010 8:22 am 
Offline

Joined: Fri Sep 30, 2005 1:53 pm
Posts: 914
Location: Philadelphia, PA
Paul and Tim,

I myself have run into MsgBox problems with something related to graphics (maybe Canvas1.Paint?), but that's not a problem which shows up when I use the technique of re-doing a Project from scratch by commenting out all the Methods (Functions and Subs) and adding a MsgBox at the beginning of each Method telling the location.

That's the approach I use when I have to more or less completely re-do a Project, putting the pieces back (removing the comment symbols), one by one.

Yes, there's a MsgBox in each Function and Sub, but it's only there when the rest of that Method (i.e., the code) is commented out. When I bring individual subs back into my Project by removing the comment symbols, I also remove the MsgBox, which thus eliminates any possible MsgBox problems. (I check out the code, of course, before I remove all the comment symbols.)

Part of the reason why I use the MsgBox, I think, is that I've never really learned how to properly use the Debugger. What I often use the MsgBox for is to tell me the size and the values of the elements of an array. (I did that yesterday when working on a Multiple Choice Test program.)

Exactly what does one do when one wants to check on array UBound and element values while stepping through the program? (I had to look at three arrays, TotalLeft(), Choice(), and TempArray, to check both the UBound and the values of all of the elements of the arrays.)

Barry Traver


Top
 Profile  
Reply with quote  
 Post subject: Re: MsgBox and Debugging
PostPosted: Sun Mar 21, 2010 6:28 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
When you break into the debugger, you get a list of variables on the right. Initially, this will be the local variables. There is a drop-down at the top of the list that enables you to navigate to more global locations.

Arrays will show up in this list like

TotalLeft(8)

which indicates that the Ubound is 8. If you want to see the contents of the array, just click on it. The debugger is a very powerful tool. It's not perfect, but it is very useful.


Top
 Profile  
Reply with quote  
 Post subject: Re: MsgBox and Debugging
PostPosted: Sun Mar 21, 2010 7:38 pm 
Offline

Joined: Fri Sep 30, 2005 1:53 pm
Posts: 914
Location: Philadelphia, PA
Tim,

Got it! Thanks. The only problem I had (and it was temporary) was finding the right things to click to get TotalLeft() to show up on the screen. Once it was there, finding the UBound and the valuse of the array elements was no problem.

I think you've dramatically altered my method of debugging. Displaying the array element values was awkward using MsgBox (even with a special Method defined to assist in that objective), but the RB Debugger is much friendlier than I had thought.

Again, thanks for the explanation. In the past, I was fearful of using the RB Debugger, but in the future I expect I'll use it frequently.

Barry Traver


Top
 Profile  
Reply with quote  
 Post subject: Re: MsgBox and Debugging
PostPosted: Mon Mar 22, 2010 12:55 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Awesome! Good to hear it.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group