Visual Basic Concepts

Verifying Your Code with Assertions

See Also

Assertions are a convenient way to test for conditions that should exist at specific points in your code. Think of an Assert statement as making an assumption. If your assumption is True, the assertion will be ignored; if your assumption is False, VB will bring it to your attention.

In Visual Basic, assertions take the form of a method: the Assert method of the Debug object. The Assert method takes a single argument of the type Boolean which states the condition to be evaluated. The syntax for the Assert method is as follows:

Debug.Assert(boolean expression)

A Debug.Assert statement will never appear in a compiled application, but when you're running in the design environment it causes the application to enter break mode with the line containing the statement highlighted (assuming that the expression evaluates to False). The following example shows the Debug.Assert statement:

Debug.Assert Trim(CustName) = "John Doe"

In this case, if the CustName isn't John Doe, the application will enter break mode; otherwise the execution will continue as usual. Using Debug.Assert is similar to setting a watch with the Break When Value Is True option selected, except that it will break when the value is false.