Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do you watch variable contents to debug? 1

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB

Till now I've been running my app then putting in showmessages, setting label.captions etc to see what's going on when something doesn't work.
I get there in the end, but sometimes it can be pesky.

So I googled around, and it seems you can add variables to the watch list.
I tried this but it just says all of the ones I want to watch are unavailable 9or something can't recall now-deleted them again).

Can I, and if so how, run the program from the IDE and have a list of the my variables showing their current contents/states, which change as I drive the program?


Steve (Delphi 2007 & XP)
 
click on the variable and press Ctrl+F5. That will put the variable you're on in your "Watch List". If you want to watch another variable, press Ctrl+F5 on that too and it will add it as well. Sometimes that variable, however will be out of scope.

For example if you've got the following code:

int global;

void __fastcall MyFunction()
{
int intNumberOne=999;
global=3;
}

void __fastcall MySecondFunction()
{ //<- this is the beginning of this particular SCOPE
String myString;
global=6031769;
} // <- this is the end of this particular SCOPE

Let's say you've got all three variables, global, intNumberOne, and myString in the "Watch List"

Now you step through code (by setting a breakpoint using F5 when you've got your point where we're declaring intNumberOne.) when MyFunction is called then you'll see that you're arrow is inside MyFunction(). Now take a look at your Watch List...

myString will be out of scope, because that variable only exists within that function.
However, global and intNumberOne will show their values. Because global is global, and therefore can be seen everywhere. And since you're in MyFunction() intNumberOne 'exists'. Once you exit that function, intNumberOne essentially is invisible to the program.


When MySecondFunction() is called somewhere, and if you step through that...

intNumberOne will be out of scope because you're no longer in MyFunction().
global will still exist, since it's a global variable, so therefore it is within scope still.
myString will now "exist" to the program because you're within the scope.

A scope is anything that exists within the brackets { and } so since global isn't in one of those, that means it can be seen everywhere. Since the other two are in {}s they can only exist within those. Hence the reason you're getting the 'out of scope' warnings. If you don't 'step' through the code, it's very likely that will not see any values because it will process a function so fast it'll pass by you. Set breakpoints (press F5) on the values you want to see, and when the program runs to that point it will freeze so you can see what's what. And if you'd like you can even roll your mouse over the variables while it's frozen and it will pop up with hints that show you what the variable equals at that moment (much like the "Watch List"). When you're done looking, either press F8 to go to the next line or press F9 to run your program again.

Hope it helps!

Cyprus
 

Thanks Cyprus that helped a lot !

You've given me a Eureka moment :)

I can't believe I've struggled on the 'old fashioned' way for so long. I'm not used to such useful gadgetry!

Steve (Delphi 2007 & XP)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top