VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Using VB Compiler Directives

by Shawn Elliott (3 Submissions)
Category: Coding Standards
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (11 Votes)

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.

Rate Using VB Compiler Directives




Using VB Compiler Directives





16.0pt;mso-bidi-font-size:12.0pt'>Using VB Compiler Directives


 


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


 


mso-bidi-font-size:12.0pt;color:navy'>Ifmso-bidi-font-size:12.0pt'> DebugMode = true then


mso-bidi-font-size:12.0pt'>                Msgbox
“The Variable value is “ & SomeVar, vbokonly, “Debug”


mso-bidi-font-size:12.0pt;color:navy'>End if


 


Or even the following


 


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


color:#339966'>                ‘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.yes">  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.yes">  How can we use a Compiler Directive to make this code more
efficient?


 


mso-bidi-font-size:12.0pt'>- THIS -


mso-bidi-font-size:12.0pt;color:navy'>Ifmso-bidi-font-size:12.0pt'> DebugMode = true then


mso-bidi-font-size:12.0pt'>                Msgbox
“The Variable value is “ & SomeVar, vbokonly, “Debug”


mso-bidi-font-size:12.0pt;color:navy'>End if


mso-bidi-font-size:12.0pt'> 


mso-bidi-font-size:12.0pt'>- CAN BE CHANGED TO -


mso-bidi-font-size:12.0pt'>#Const DebugMode =
true


mso-bidi-font-size:12.0pt'> 


mso-bidi-font-size:12.0pt'>#If DebugMode = true
then


mso-bidi-font-size:12.0pt'>                Msgbox
“The Variable value is “ & SomeVar, vbokonly, “Debug”


mso-bidi-font-size:12.0pt'>#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.yes">  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.


 


color:#339966'>‘Programmer needs to determine what kind of code he is trying to
create by defining the


color:#339966'>‘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


color:#333399'> 


color:#333399'> 


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


 





Download this snippet    Add to My Saved Code

Using VB Compiler Directives Comments

No comments have been posted about Using VB Compiler Directives. Why not be the first to post a comment about Using VB Compiler Directives.

Post your comment

Subject:
Message:
0/1000 characters