Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources]

Alison Balter

نسخه متنی -صفحه : 544/ 263
نمايش فراداده

Using Assertions

You use assertions to ensure that, if the user encounters a certain state, your code invokes the debugger. The following code, found in basExamples, is an example:

Sub Assertion() Dim intAge As Integer intAge = InputBox("Please Enter Your Age") Debug.Assert (intAge >= 0) MsgBox "You are " & intAge End Sub

This example sets the value of a variable called intAge equal to the value entered into an Input Box. The Debug.Assert statement "asserts" that the value entered is greater than or equal to zero. If it is, code execution proceeds as expected. If the assertion is

incorrect, the code invokes the debugger.

It is a good idea to include a comment as to why an assertion might fail. By doing this, you will facilitate the process of responding to the situation when it occurs. Also, it is important to realize that, if you deploy your application with Debug.Assert statements intact, you will receive a technical support call when an assertion fails, and your code places the user in the debugger without warning!