<

Go Premium for a chance to win a PS4. Enter to Win

x

Conditional Compilation

Published on
14,674 Points
4,974 Views
7 Endorsements
Last Modified:
Martin Liss
Over 40 years of programming experience. Expand my "Full Biography" to see links to some articles I've written.
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)
#Else 
    ' Do something else
#End If

Open in new window


I find it particularly useful when doing things like that to set up a 'Conditional Compilation Argument' by going to Project|<MyProgramName|Properties|Make 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
#End If

Open in new window


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
    #End If
    
End Sub

Open in new window


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>.
#Else
   ' <code specific to other versions>.
#End If
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!
7
Comment
Author:Martin Liss
0 Comments

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Join & Write a Comment

Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month