- VB.Net - Event Handling
- VB.Net - Advanced Forms
- VB.Net - Dialog Boxes
- VB.Net - Basic Controls
- VB.Net - File Handling
- VB.Net - Exception Handling
- VB.Net - Classes & Objects
- VB.Net - Subs
- VB.Net - Functions
- VB.Net - Collections
- VB.Net - Arrays
- VB.Net - Date & Time
- VB.Net - Strings
- VB.Net - Loops
- VB.Net - Decision Making
- VB.Net - Operators
- VB.Net - Directives
- VB.Net - Statements
- VB.Net - Modifiers
- VB.Net - Constants
- VB.Net - Variables
- VB.Net - Data Types
- VB.Net - Basic Syntax
- VB.Net - Program Structure
- VB.Net - Environment Setup
- VB.Net - Overview
- VB.Net - Home
VB.Net Advanced Tutorial
- VB.Net - Web Programming
- VB.Net - XML Processing
- VB.Net - Send Email
- VB.Net - Excel Sheet
- VB.Net - Database Access
- VB.Net - Regular Expressions
VB.Net Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 pne. 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. Unpke C and C++ directives, they are not used to create macros.
Compiler Directives in VB.Net
VB.Net provides the following set of compiler directives −
The #Const Directive
The #ExternalSource Directive
The #If...Then...#Else Directives
The #Region Directive
The #Const Directive
This directive defines conditional compiler constants. Syntax for this directive is −
#Const constname = expression
Where,
constname − specifies the name of the constant. Required.
expression − it is either a pteral, or other conditional compiler constant, or a combination including any or all arithmetic or logical operators except Is.
For example,
#Const state = "WEST BENGAL"
Example
The following code demonstrates a hypothetical use of the directive −
Module mydirectives #Const age = True Sub Main() #If age Then Console.WriteLine("You are welcome to the Robotics 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 Robotics Club
The #ExternalSource Directive
This directive is used for indicating a mapping between specific pnes 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 including external code from an external code file into a source code file.
Syntax for this directive is −
#ExternalSource( StringLiteral , IntLiteral ) [ LogicalLine ] #End ExternalSource
The parameters of #ExternalSource directive are the path of external file, pne number of the first pne, and the pne where the error occurred.
Example
The following code demonstrates a hypothetical use of the directive −
Module mydirectives Pubpc Class ExternalSourceTester Sub TestExternalSource() #ExternalSource("c:vbprogsdirectives.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
When the above code is compiled and executed, it produces the following result −
This is External Code. In Main.
The #If...Then...#Else Directives
This directive conditionally compiles selected blocks of Visual Basic code.
Syntax for this directive is −
#If expression Then statements [ #ElseIf expression Then [ statements ] ... #ElseIf expression Then [ statements ] ] [ #Else [ statements ] ] #End If
For example,
#Const TargetOS = "Linux" #If TargetOS = "Windows 7" Then Windows 7 specific code #ElseIf TargetOS = "WinXP" Then Windows XP specific code #Else Code for other OS #End if
Example
The following code demonstrates a hypothetical use of the directive −
Module mydirectives #Const classCode = 8 Sub Main() #If classCode = 7 Then Console.WriteLine("Exam Questions for Class VII") #ElseIf classCode = 8 Then Console.WriteLine("Exam Questions for Class VIII") #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 VIII
The #Region Directive
This directive helps in collapsing and hiding sections of code in Visual Basic files.
Syntax for this directive is −
#Region "identifier_string" #End Region
For example,
#Region "StatsFunctions" Insert code for the Statistical functions here. #End RegionAdvertisements