The VFP ASSERT statement

FoxPro Wait window as debugger. Although Visual FoxPro has a good debugger, there are times when you need a little more information about what's going on - or perhaps you need to be able to see what's going on without the intrusive overhead of the debugger. The classic technique from the days of DOS is to use a WAIT WINDOW command to display values at the crucial point in the program and this is still a useful technique.

The screen shot shows a Wait window. It's not pretty and it's not part of the regular Windows user experience but it's still useful. Having said that, it's not the sort of thing that you want a user to see too often. You can write code to detect whether you're developing or running the program and only show the window in the programming environment but the Assert mechanism is sometimes easier.

Visual FoxPro Asserts

The traditional use of an Assert in more rigorous languages such as C++ or Java is as a guarantee that the program is indeed in the state which the programmer expects it to be in. FoxPro too allows an Assert to be used as a tool for Quality Assurance in this way. This example for instance shows that the programmer expects the Customer table to be open and to be indexed in Name order at this point;

Set Asserts On
...
Assert Used("Customer") ;
     Message "The customer table must be open."
Assert Order ("Customer") = "Name";
     Message "Table must be in Name Order."

If the customer table is not open or it's not in the right order then there's no point in letting program execution continue. The developer needs to find out why these conditions have not been met. Without the Assert, the program would have continued to run until it reached the point where it needed to use the Customer table and that might have been several hundred lines of code away, possibly in another method or function altogether. The Assert statement means that the program will stop as soon as we know that something is going wrong, it doesn't allow the error to propagate across the application.

VFP Assert message. The general form of the command is the Assert keyword followed by a boolean expression. If the expression evaluates as false then FoxPro will pause execution and display a dialog giving the programmer the options of opening the debugger, cancelling the program or ignoring the failure. The example here makes use of the optional Message clause to display some programmer-friendly text. Without it we'd just get a generic "Assertion failed on 23 of procedure myProg."

Using Assert to debug

The key word in the previous sentence is 'programmer' because the assert mechanism does not operate at runtime. This means that you should not be tempted to try to use Asserts as a mechanism for validation during data entry at runtime. The assert will work for you as a programmer in the development environment but not for the user.

An Assert is meant to be used as an aid to software quality control, to ensure that the developer has got the program environment set up correctly. The fact that asserts don't fire at runtime does mean that we can use Assert as a sneaky way of showing us debug information. We can be certain that the user won't see these messages. All we have to do is to write an Assert statement with a boolean expression that will evaluate as false. Something as simple as this:

Set Asserts On
...
Assert .F. ;
     Message "Postcode is " + customer.postalcode

will show the debug message to the developer but not to the user. If you want to temporarily suppress the messages in the development environment too then you can just turn asserts off:

Set Asserts Off

Note that the default is for asserts to be off. You must have a Set Asserts On statement if you want to use asserts at all.

MS Access technical tips

Visual FoxPro technical tips

General Tips

 

More tips from Alvechurch Data

Alvechurch Data - Microsoft FoxPro and Access Database Development and Training

Alvechurch Data are based close to Birmingham and provide Microsoft Access and Visual FoxPro training, development and support.

Read More

Autocomplete in Visual Fox Pro

Autocomplete in Visual Fox Pro

The textbox class in Visual FoxPro 9 has a new Autocomplete property which shows the user the previous values that have been entered in that textbox.

Read More

Access and Visual FoxPro Training

Microsoft Access and Visual FoxPro training courses and workshops offered by Alvechurch Data in Warwickshire.

Read More

Geoff Franklin CV

Geoff Franklin - resume of education and experience in Access and Visual FoxPro programming.

Read More

Visual FoxPro Frequently Asked Questions

Frequently asked questions for Visual FoxPro

Read More