Table of contents
Collapse the table of content
Expand the table of content

Assert Method

office 365 dev account|Last Updated: 6/12/2017
1 Contributor

Conditionally suspends execution at the line on which the method appears. Syntaxobject.Assertbooleanexpression The Assert method syntax has the following object qualifier and argument:

objectRequired. Always the Debug object.
booleanexpressionRequired. An expression that evaluates to either True or False.

RemarksAssert invocations work only within thedevelopment environment. When the module is compiled into an executable, the method calls on the Debug object are omitted. All of booleanexpression is always evaluated. For example, even if the first part of an And expression evaluates False, the entire expression is evaluated.


The following example shows how to use the Assert method. The example requires a form with two button controls on it. The default button names are Command1 and Command2.

When the example runs, clicking the Command1 button toggles the text on the button between 0 and 1. Clicking Command2 either does nothing or causes an assertion, depending on the value displayed on Command1. The assertion stops execution with the last statement executed, the Debug.Assert line, highlighted.

Option Explicit
Private blnAssert As Boolean
Private intNumber As Integer

Private Sub Command1_Click()
    blnAssert = Not blnAssert
    intNumber = IIf(intNumber <> 0, 0, 1)
    Command1.Caption = intNumber
End Sub

Private Sub Command2_Click()
    Debug.Assert blnAssert
End Sub

Private Sub Form_Load()
    Command1.Caption = intNumber
    Command2.Caption = "Assert Tester"
End Sub
© 2018 Microsoft