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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I use ASSERTS to test for errors?

Debugging and Tuning

How do I use ASSERTS to test for errors?

by  foxdev  Posted    (Edited  )
If you're like most of us, you're used to putting in numerous [tt]WAIT WINDOW[/tt] and [tt]? MYVAR[/tt] statements throughout your code to try to track down problems.

Now that we have a decent debug/trace ability in VFP, you can set breakpoints and [tt]SET STEP ON[/tt] in various places. But sometimes the problem occurs infrequently, and you hate stepping through hundreds of lines of code.

Enter ASSERT. An ASSERT allows you to test an expression at runtime, and only notify you if the expression evaluates to false. As a bonus, it not only notifies you, but allows you to invoke the debugger at that point.

And as an extra special bonus: raise your hand if you've ever accidently left debugging code (like a WAIT WINDOW) in an application that went into production. I've done it dozens of times, and its darn embarrassing. Fortunately, if you issue the command [tt]SET ASSERTS OFF[/tt] then ASSERT statements aren't executed at runtime.

Note that ASSERTS are OFF by default, so you'll have to turn them on by issuing the command SET ASSERTS ON.

Some examples of using Asserts:

[tt]nConnHandle = sqlconnect('myconnection')
assert nConnHandle>0 message "connection failed"

select * from users where userid='FRED' into cursor curUsers
assert reccount('curUsers') > 0 message "no users found"[/tt]

The [tt]message[/tt] clause is optional, but is suggested because the default message is assert failed, which is not terribly descriptive.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top