What is VB.net Directives?
The VB.net Compiler Directives give instructions to the compiler to preprocess the information before actual compilation starts.
All these directives begin with #, and only white-space characters may appear before a directive on a line. These directives are not statements.
VB.net compiler does not have a separate preprocessor; however, the directives are processed as if there was one.
In VB.net, the compiler directives are used to help in conditional compilation. Unlike C and C++ directives, they are not used to create macros.
Compiler Directives in VB.net
In VB.net, the following types of directives are available.
- #Const Directive
- #ExternalSource Directive
- #If…Then…#Else Directives
- #Region Directive
#Const Directive
The #Const directive defines conditional compiler constants, and these constants are always private to the file in which they appear.
Syntax for #Const Directive:
#Const constname = expression
Constname
specifies the name of the constant. Required.
Expression
it is either a literal, or other conditional compiler constant or a combination including any or all arithmetic or logical operators except Is.
For Example:
#Const state = “Philippines”
Example Program:
'example program for Const Directive'
Module mydirectives
#Const age = True
Sub Main()
#If age Then
Console.WriteLine("You are welcome to the Club")
#End If
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces the following result:
You are welcome to the Club
You can test the above example here! ➡ VB.net Online Compiler
#ExternalSource Directive
This directive is used for indicating a mapping between specific lines of source code and text external to the source.
It is used only by the compiler and the debugger has no effect on code compilation.
This directive allows the inclusion of external code from an external code file into a source code file.
Syntax for #ExternalSource Directive:
#ExternalSource( StringLiteral , IntLiteral )
[ LogicalLine ]
#End ExternalSource
The parameters of the #ExternalSource directive are the path of the external file, the line number of the first line, and the line where the error occurred.
Example Program:
The following code demonstrates a hypothetical use of the directive.
Module mydirectives
Public Class ExternalSourceTester
Sub TestExternalSource()
#ExternalSource("c:\vbprogs\directives.vb", 5)
Console.WriteLine("This is External Code. ")
#End ExternalSource
End Sub
End Class
Sub Main()
Dim t As New ExternalSourceTester()
t.TestExternalSource()
Console.WriteLine("In Main.")
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces the following result:
This is External Code.
In Main.
You can test the above example here! ➡ VB.net Online Compiler
#If…Then…#Else Directives
The #If…Then…#Else directive conditionally compiles selected blocks of Visual Basic code.
The behavior of the #If…Then…#Else directives appear the same as that of the If…Then…Else statements.
However, the #If…Then…#Else directives evaluate at compile-time, whereas the If…Then…Else statements evaluate conditions at run time.
Syntax for #If…Then…#Else directive:
If expression Then
statements
[ #ElseIf expression Then
[ statements ]
…
ElseIf expression Then
[ statements ] ]
[ #Else
[ statements ] ]
End If
Example Program:
Module mydirectives
#Const classCode = 5
Sub Main()
#If classCode = 5 Then
Console.WriteLine("Exam Questions for Class V")
#ElseIf classCode = 6 Then
Console.WriteLine("Exam Questions for Class VI")
#Else
Console.WriteLine("Exam Questions for Higher Classes")
#End If
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces the following result:
Exam Questions for Class V
You can test the above example here! ➡ VB.net Online Compiler
#Region Directive
This directive helps in collapsing and hiding sections of code in Visual Basic files.
Syntax of #Region Directive:
Region “identifier_string”
End Region
Example:
#Region "StatsFunctions"
' Insert code for the Statistical functions here.
#End Region
Summary
Directives add another layer of compilation to VB.NET programs.
They can be considered a program on top of an actual VB.NET program. With #If, #Const, and even #Region, we access another set of abilities in this language.
< PREVIOUS
NEXT >