Debug.Assert Method

Syntax

object.Assert booleanexpression

object

Use: Required

Data Type: Debug object

Always evaluates to the Debug object.

booleanexpression

Use: Required

Data Type: Boolean

Expression that evaluates to a Boolean value.

Return Value

None.

Description

Conditionally suspends program execution and transfers control to the Immediate window if the value of booleanexpression is False.

Rules at a Glance

  • booleanexpression must evaluate to a Boolean value.

  • If booleanexpression is False, program execution is suspended and control transfers to the Immediate window.

  • Program execution can be resumed by pressing the Continue button on either the Standard or Debug toolbars.

Programming Tips and Gotchas

  • The Assert method is available only in Visual Basic; the Debug object in the VBA development environment doesn't support the Assert method.

  • Assert is typically used when debugging to test an expression that should evaluate to True. If it doesn't, the Immediate window can be used to investigate why the test failed.

  • Debug.Assert executes only when an application is run in the design-time environment; the statement has no effect in a compiled application. This means that Debug.Assert never produces a runtime error if the call to it is inappropriate, nor does it suspend program execution outside of the VB IDE. Because of this, you don't have to remove Debug.Assert statements from finished code or separate them with conditional #If...Then statements.

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.