Search Tools Links Login

Using VB Compiler Directives


This Tutorial will allow you to use Visual Basic Compiler Directives such as
#IF..#Elseif..#Else..#End If
and
#Const
to create more robust and maintanable code.

Original Author: Shawn Elliott

Code




Using VB Compiler Directives





Using VB Compiler Directives


 


I don’t know how many times I have seen the following code


 


If DebugMode = true then


                Msgbox
“The Variable value is “ & SomeVar, vbokonly, “Debug”


End if


 


Or even the following


 


style='mso-tab-count:1'>                ‘Uncomment
the following to debug this var


                ‘Msgbox
“The Variable value is “ & SomeVar, vbokonly, “Debug”
style='color:lime'>


 


Many Visual Basic Programmers are not using one of the
powerful features of VB that equate it with other programming languages.


 


Compiler Directives.


 


“What are Compiler Directives?”


Well, Compiler Directives are small
instructions that determine whether or not a piece of code will be included in
the Compile and Link process of creating an executable.


 


“What are the Compiler Directives to
use in VB?”


In Visual basic you get the
following


            #style='color:navy'>Const


This is private in the module it is defined.style="mso-spacerun: yes">  The Const items are NOT global to the
project only in their specific scope such as a form or class module


            #style='color:navy'>If


                        This
is used to evaluate an expression of type #Const
= n-expression


            #style='color:navy'>Elseif


                        This
is used to evaluate an expression of type #Const
= n-expression within and #IF block


            #style='color:navy'>Else


                        Code
within this sub-block is compiled if the #IF
and #ELSEIF blocks all evaluated to false


            #style='color:navy'>End If


                        This
ends the #IF Compiler Directive Block


 


“Why would I want to use Compiler
Directives?  I have If Then Statements”


            If you
believe in adding additional code and using additional memory as well as CPU
cycles then Compiler Directives are not for you.  Not to mention having Debug code or unwanted code in your final
exe simply because you forgot to comment one small line of code.style="mso-spacerun: yes"> 


 


VB Specified Constants


            Visual
Basic defines some Compiler Constants automatically for you.style="mso-spacerun: yes">  These are:


                        Win16style='mso-tab-count:1'>  “This indicates that the development
environment is 16-bit”


                        Win32style='mso-tab-count:1'>  “This indicates that the development
environment is 32-bit”


 


How to use Compiler Directives


            Let’s take
a look at the first set of code we examined. 
We notice it is a simple if then determining if the program needs to
show a Message Box with the value of a variable.  How can we use a Compiler Directive to make this code more
efficient?


 


- THIS -


If DebugMode = true then


                Msgbox
“The Variable value is “ & SomeVar, vbokonly, “Debug”


End if


 


- CAN BE CHANGED TO -


#Const DebugMode =
true


 


#If DebugMode = true
then


                Msgbox
“The Variable value is “ & SomeVar, vbokonly, “Debug”


#End If


 


Notice there was no real change in the code except we used
the #IF directive and ended the statement block with the #END IF statement to
check the value of a special Compiler Variable (which was defined with the
#CONST block)


These work the sameway as the If…Else…Elseif…End If
statement we are all used to except for one thing.  If the condition being tested doesn’t prove true then the code
inside the block WILL NOT be included in the outputted code.style="mso-spacerun: yes"> 


 


Other uses beside Debug


            The most
common use for Compiler Directives in other languages such as C and C++ are to
define sets of code for different operating systems and versions.style="mso-spacerun: yes">  We can do the same thing with visual basic.


 


‘Programmer needs to determine what kind of code he is trying to
create by defining the


‘target OS Version here


#style='color:#333399'>Const OSVersion = “Win9X”


 


 


 


#style='color:#333399'>If OSVersion = "Win9X" style='color:#333399'>Then


style='mso-tab-count:1'>                ‘Programmer
needs to put specific Windows 95, 98 code here


#style='color:#333399'>ElseIf OSVersion = "WinNT" style='color:#333399'>Then


style='mso-tab-count:1'>                ‘Programmer
needs to put specific Windows NT code here


#style='color:#333399'>ElseIf OSVersion = "Win2K" style='color:#333399'>Then


style='mso-tab-count:1'>                ‘Programmer
needs to put specific Windows 2000 code here


#style='color:#333399'>Else


style='mso-tab-count:1'>                ‘Programmer
has not defined the OS Version


#style='color:#333399'>End If


 


 


Final Notes


            An
important thing to remember is that #Const and
Const variables cannot be interswitched. 
If you try to use a #Const variable in place of a const or a const in
place of a #Const variable VB will give you
Syntax errors.


 


            Also very
important is to Remember the scope of the #Const
variable.  It is only within it’s module
like a Form, Module or Class Module.


 


Shawn Elliott


 





About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 103 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.