Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

VB.Net - Instructions


May 13, 2021 vb.net


Table of contents


Vb. Net compiler instructions are given to compilers to preprocess information before the actual compilation begins.

All of these instructions begin with a . . . and only space characters appear before an instruction on a line. These instructions are not statements.

Vb. N et compilers do not have separate preprocessors; H owever, the instruction is processed as if there were one. I n VB.Net, compiler instructions are used to help the condition compile. Unlike the C and C instructions, they are not used to create macros.


The VB.Net directive in the file

Vb. Net provides the following set of compiler instructions:

  • The #Const directive

  • The #ExternalSource directive

  • The #If... Then... #Else instructions

  • The #Region directive


The .Const Directive

The instruction defines the conditional compilation constant. T he syntax instruction is:

#Const constname = expression


  • constname: Specifies the name of the constant. N ecessary.

  • expression : It is a text or other conditional compiler constant, or contains any or all arithmetic or logical operators (except Is).


For example

#Const state = "WEST BENGAL"


Example

The following code demonstrates the use of pseudo-instructions:

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 results:

You are welcome to the Robotics Club


The externalSource directive

This directive uses to indicate the mapping between the source code of a particular line and the external text of the source. /b117> It uses only compiled by the compiler and debugger and does not affect the code.

This directive allows code that includes from outside the external code file to a source code file.

The syntax of this directive is ..

#ExternalSource( StringLiteral , IntLiteral )
    [ LogicalLine ]
#End ExternalSource

#Externalsource directive parameters are the path to the external file, The line number of the first line and the line where the error occurred.


Example


The following code demonstrates the use of pseudo-instructions:

Module mydirectives
    Public 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 results:

This is External Code.
In Main.


The #If... Then... #Else instructions

This pseudo-instruction conditionally compiles the selected Visual Basic block of code.

The syntax of this pseudo-instruction 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 instruction:

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 results:

Exam Questions for Class VIII


The #Region Directive

This pseudo-instruction helps to collapse and hide snippppy code in the Visual Basic file.

The syntax of this pseudo-instruction is:

#Region "identifier_string" 
#End Region


For example

#Region "StatsFunctions" 
    ' Insert code for the Statistical functions here.
#End Region