Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar
and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Assert which when used in a line of code like Debug.Assert MyBoolean
will cause a break in the program at that line if MyBoolean is True. Both Debug statements will be ignored when the program is compiled so they are superior to MsgBox statements which need to be removed before a program is compiled, unless, of course, you want the user to know the value of MyVar.
Debug statements are useful but limited However there is another debugging method that is less well known and that is conditional compilation. Conditional compilation uses special #!f, #Else, #ElseIf and #End If reserved words that like Debug are not included in compiled code. This can be very handy in debugging where you can do something like
#If MyVar > 5 Then ' or the value of some Boolean is True, etc.
' Do something (or any number of things)
' Do something else
I find it particularly useful when doing things like that to set up a 'Conditional Compilation Argument
' by going to Project|<MyProgramName|Pro
ke and putting something like Testing = -1
in the 'Conditional Compilation Arguments:' box. Note that -1 or any non-zero value equates to True and that Conditional Compilation arguments have global scope. In other words they are available anyplace in your program. Once I've done that I can do something like the following which is code from one of my apps.
#If Testing Then
picSmilie(0).Visible = True
picSmilie(picSmilie.Count - 1).Top = picSmilie(picSmilie.Count - 2).Top - 40
picSmilie(picSmilie.Count - 1).Visible = True
You don't have to understand exactly what that does except to know that it alters what appears in the program when I'm testing it.
It's important to realize that unless Testing is changed to 0 (False) before compilation that that code will be compiled into the program and that is probably not what you want. There's an easy fix for that however and that is to add a Sub like the following.
Public Sub DoNotAllowTestCodeToCompile()
#If Testing Then
Do not compile
If Testing is left True then the #If/#Else the compiler will stop with an error since Do not compile
is not a valid line of code:) If you have already changed Testing to 0 then the code in the Sub will be ignored and barring other errors the code will compile.
One final note and that is that while conditional compilation can be a valuable testing tool, there are uses for it that don't involve testing at all like in this explanation and code from the MSDN Library.
For example, to create French language and German language versions of the same application from the same source code, embed platform-specific code segments in #If...Then statements using the predefined constants conFrenchVersion and conGermanVersion.
#If conFrenchVersion Then
' <code specific to the French language version>.
#ElseIf conGermanVersion then
' <code specific to the German language version>.
' <code specific to other versions>.
If the value of the conFrenchVersion constant is set to True at compile time, the conditional code for the French language version will be compiled. If the value of the conGermanVersion constant is set to True, the compiler uses the German language version.
If you find that this article has been helpful, please click the “thumb’s up” button below. Doing so lets me know what is valuable for EE members and provides direction for future articles. It also provides me with positive feedback in the form of a few points. Thanks!