Search Tools Links Login

Using VB Compiler Directives


Visual Basic 6, or VB Classic

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: 116 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.