If you want to run a project as an end-user would (without calls to the Debug object, without stopping at breakpoints, and so forth), select Start Without Debugging from the Debug menu or press Ctrl+F5. Doing so disables the debugger so that you see exactly what an end-user would see, and you don't have to concern yourself with commenting Debug calls and disabling breakpoints.
When writing an expression for Debug.Assert, make sure that the expression doesn't alter any data within the procedure. If it does, the results you experience while debugging might not be the same as those experienced by a user of your program. To summarize (not so well) the Heisenberg uncertainty principle of quantum physics: the presence of the observer changes the nature of the observed (that and something about a dead cat in an unopened box). Forget about the cat for a moment and consider the following statement:
Debug.Assert(CustomerInterestRate(lngCustomerID) > 8.5)
If the function CustomerInterestRate modifies any module, global, or static data or if it persists information to a data store, the Debug.Assert call actually changes the state of the application. This change of state won't be present in the build code, however, because Debug calls aren't compiled into distributable components. Essentially, you should avoid making any function calls (even apparently "safe" ones) when calling Debug.Assert.
' Assert if more than 50 users are in the database. Debug.Assert(TestConcurrentUsers() > 50)
' Assert if more than 50 users are in the database. ' Note that the behavior of the release version will ' be the same as the debug version, because the function ' is called in both. Dim intUserCount As Integer = TestConcurrentUsers() Debug.Assert(intUserCount > 50)
When you create an assertion that evaluates a complicated expression, specify optional text to display if the assertion occurs. That way, the purpose of the assertion becomes obvious to any developer that receives the assertion.
Debug.Assert(objFont.Name = "Courier" And objFont.Size < 8)
Debug.Assert(objFont.Name = "Courier" And objFont.Size < 8, _ "Point size too small for Courier font!")
Although this was covered in detail in Chapter 5, "Variables," it bears repeating here. With Option Strict turned on, your code will contain fewer bugs because the compiler takes on some of your debugging duties.
In Chapter 10, "Exception Handling," I discussed in detail how to create exception-handling code. The more robust your error handlers, the easier it is to isolate and identify bugs.
Earlier in this chapter, I noted that Visual Basic .NET includes a built in compiler constant: DEBUG. When creating conditionally compiled debug code, use this predefined constant than rather than create one of your own.
#Const IncludeDebugCode = True #If IncludeDebugCode Then ' Place debug code here. #End If
#If DEBUG Then ' Place debug code here. #End If
One of the most common debugging mistakes is not debugging all of a procedure. When you write a complex procedure with many If End If or Select Case structures, it's relatively easy to miss testing all of the code because of the necessity to create all of the conditions to hit all branches of code. One approach to this problem is to place a breakpoint in each of the conditional blocks of code (after every If statement, Else statement, and so on). As execution hits a breakpoint, remove the breakpoint. When you've removed all of the breakpoints in the procedure, you know that you've tested the code in all possible conditions within the procedure.
A Stop statement halts code execution and enters Break mode much like a breakpoint. The primary difference is that Stop statements get compiled into release code. A Stop statement can drop a user to the desktop one of the worst possible scenarios. For this reason, it's best to use a breakpoint in place of a Stop statement whenever possible. The breakpoint won't compile into the release code, and you can use the Breakpoints window to easily locate and manage the breakpoint.
One way to create a task in the Task List is to create a comment that begins with the word TODO. Such comments are automatically added to the Task List as type Comment. You can filter the Task List for comments by right-clicking the Task List and choosing Comment from the Show Tasks submenu as shown in Figure 13-29. TODO tasks provide a consistent mechanism for noting and addressing issues in your code.