Using VB Compiler Directives
![Visual Basic 6, or VB Classic Visual Basic 6, or VB Classic](https://www.fortypoundhead.com//assets/img/icons/v7/vb6.png)
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 I don’t know how many times I have seen the following code If DebugMode = true then ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á Msgbox End if Or even the following style='mso-tab-count:1'>?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á ‘Uncomment ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á ‘Msgbox Many Visual Basic Programmers are not using one of the Compiler Directives. “What are Compiler Directives?” Well, Compiler Directives are small “What are the Compiler Directives to In Visual basic you get the ?á?á?á?á?á?á?á?á?á?á?á #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 ?á?á?á?á?á?á?á?á?á?á?á #style='color:navy'>If ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á This ?á?á?á?á?á?á?á?á?á?á?á #style='color:navy'>Elseif ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á This ?á?á?á?á?á?á?á?á?á?á?á #style='color:navy'>Else ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á Code ?á?á?á?á?á?á?á?á?á?á?á #style='color:navy'>End If ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á This “Why would I want to use Compiler ?á?á?á?á?á?á?á?á?á?á?á If you ?á?á?á?á?á?á?á?á?á?á?á Visual ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á Win16style='mso-tab-count:1'>?á “This indicates that the development ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á Win32style='mso-tab-count:1'>?á “This indicates that the development ?á?á?á?á?á?á?á?á?á?á?á Let’s take - THIS - If DebugMode = true then ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á Msgbox End if - CAN BE CHANGED TO - #Const DebugMode = #If DebugMode = true ?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á Msgbox #End If Notice there was no real change in the code except we used These work the sameway as the If…Else…Elseif…End If ?á?á?á?á?á?á?á?á?á?á?á The most ‘Programmer needs to determine what kind of code he is trying to ‘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 #style='color:#333399'>ElseIf OSVersion = "WinNT" style='color:#333399'>Then style='mso-tab-count:1'>?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á ‘Programmer #style='color:#333399'>ElseIf OSVersion = "Win2K" style='color:#333399'>Then style='mso-tab-count:1'>?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á ‘Programmer #style='color:#333399'>Else style='mso-tab-count:1'>?á?á?á?á?á?á?á?á?á?á?á?á?á?á?á ‘Programmer #style='color:#333399'>End If ?á?á?á?á?á?á?á?á?á?á?á An ?á?á?á?á?á?á?á?á?á?á?á Also very
“The Variable value is “ & SomeVar, vbokonly, “Debug”
the following to debug this var
“The Variable value is “ & SomeVar, vbokonly, “Debug”style='color:lime'>
powerful features of VB that equate it with other programming languages.
instructions that determine whether or not a piece of code will be included in
the Compile and Link process of creating an executable.
use in VB?”
following
project only in their specific scope such as a form or class module
is used to evaluate an expression of type #Const
= n-expression
is used to evaluate an expression of type #Const
= n-expression within and #IF block
within this sub-block is compiled if the #IF
and #ELSEIF blocks all evaluated to false
ends the #IF Compiler Directive Block
Directives??á I have If Then Statements”
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
Basic defines some Compiler Constants automatically for you.style="mso-spacerun: yes">?á These are:
environment is 16-bit”
environment is 32-bit”How to use Compiler Directives
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?
“The Variable value is “ & SomeVar, vbokonly, “Debug”
true
then
“The Variable value is “ & SomeVar, vbokonly, “Debug”
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)
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
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.
create by defining the
needs to put specific Windows 95, 98 code here
needs to put specific Windows NT code here
needs to put specific Windows 2000 code here
has not defined the OS VersionFinal Notes
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.
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
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.