HOME - - - - - Delphi Tutorials TOC - - - - - - Other material for programmers

Delphi tutorial: Debugging.. and avoiding the need (Dt4c)

This is still in a draft form.... it is probably mostly right, but I make no promises just yet!!!

This has good information, and a search button at the bottom of the page.

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!

Click here if you want to know more about the source and format of these pages

Almost all of the Pascal Tutorial on debugging on debugging also has material that applies to working in Delphi. The section on sending yourself messages about the state of variables, using writeln, is presented in a Delphi version below.
This page is a "work in progress"... I've run out of time on the day's work, and needed to leave SOMETHING here to hold to spot. Email me to say "We hate "site under construction" pages, and I'll get back to this sooner for you!

You shouldn't need to debug your programs!! (So why is it that I always have to debug mine?) If a program is written in a disciplined, careful manner it won't need debugging. I make this perhaps obvious, perhaps sanctimonious observation to encourage you to AVOID "poke and hope" programming. Almost always, if I try to "fix" something with a little bit of somehting that hasn't been thought throuh carefully, I end up with a mess that takes longer to get working than if I'd just worked a little more deliberately in the first place.

Delphi offers great debugging tools. Master them, soon. In the meantime, just putting things like...

sTmp:='The value in bMyVariable at the moment is:'+sTmp;

... in your code will often open your eyes to what your program is REALLY doing. (As opposed to what you thought you told it to do!) Choosing the right spot to put the little message sender is an art you will master. (The fancy alternative is called a "breakpoint", and you'll want to master "stepping through" and "tracing into" your code... but the message sending system described above will help until then!) (If you were to put the above into a program, it would need to have sTmp declared as a string, and not needed for other things at the moment, and you'd have a variable of your own devising and declaring called bMyVariable in use. IntToStr is built into Delphi.)

If a bug halts your program after it has started to run, you can use Run|Evaluate to check what values are in variables. Run|Evaluate can also be used for cheking your hex/decimal conversions, looking up ascii values, etc... even simple calculations: $10+5 gives 3 because $20 means 20 base 16 is 32, and 32 plus 5 is 37. ord('A') gives 65 because the Ascii code for 'A' is 65. (Purists: don't attack me. This is a BEGINNER'S tutorial!)

Delphi, from it's roots in Pascal, is a very orderly language. The basis of that order is the "block".. a concept that rewards study. The Delphi environment helps you enormously when it comes to adding things to your code in an orderly manner. Suppose you've put a button on your form. Now you want to write the code to be executed if the button is clicked. Click on the button, if it is not already selected. Go to the Object Inspector. Click on the events tab (bottom of Object Inspector window). Click on the OnClick line... and Delphi does lots for you:
procedure TForm1.ButtonClick(Sender:TObject);


(and some other stuff in another part of the code!)
You jus ttype in the "special" stuff, mostly between the procedure's "begin" and "end". E.g.: For a button to close the application down, put application.terminate; in.

I spoke a bit about variable naming in the Pascal Tutorial on debugging. Delphi, because of the complexities inherent in Windows, has more to contend with, but life isn't impossible, even so.

Try to be in the habit of naming ovbjects as soon as you put them on your form. The rules for naming them are the same as the variable naming rules. I tend to use the following prefixes:
For labels: la
For buttons: bu
For memos: m
For menues: me
For edit boxes: e
For panels: pa

   Search this site or the web        powered by FreeFind
  Site search Web search
Site Map    What's New    Search

Click here if you're feeling kind! (Promotes my site via "Top100Borland")
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows pc) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's freeware, shareware page.

Link to Tutorials main page
Here is how you can contact this page's author, Tom Boyd.

Valid HTML 4.01 Transitional Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org

If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....