Using fsAssert



How can I use fsAssert to help debug my program?


fsAssert evaluates an expression, and prints a message to the trace log if the expression is true.You can do this at the beginning of functions to test parameter values against what is expected.You can use this in the body of a function to evaluate results, or to log expressions and their values to the trace log. An example would be:

Sub Foo(intType as integer) 
'intType should always be between 0 and 5 
Call fsAssert( intType < 0 OR intType > 5, "intType was not within range. It was : " & intType) 
'Check the result of a function 
bResult = Refresh(vData as Variant) 
Call fsAssert(bResult = false, "Refresh Failed.vData contained: " & vdata

End Sub

This would produce the following output to the trace file:

[Project1]intType was not within range. It was : 10
[Project1]Refresh Failed. vData contained: SkyView Mountain Lodge

By default, fsAssert outputs the message and the program continues. However, another possibility is to have it abort, similar to the C ASSERT instruction. To change this, uncomment the stop command in the fsAssert function in FailSafe.bas. This will make the program break in design mode, or abort in compiled mode.

Using assertions is a powerful way to check for bugs in a program. The ability to have them logged allows the developer to understand what went wrong to cause the assertion to execute.

Old KB# 10988
Comment List
Related Discussions