<

Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x

Conditional Compilation

Published on
14,563 Points
4,863 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
0 Comments

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Join & Write a Comment

Suggested Articles

As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month