Operators Conditionals And Loops

The Option and Imports Statements

The Option statement sets a number of options for the rest of your code, and the Imports statement imports namespaces into your code, making them more readily available.

Option Statements

You use Option statements to set the "ground rules" for your code, helping prevent syntax and logic errors. Here are the possibilities:

  • Option Explicit: Set to On or Off. On is the default. Requires declaration of all variables before they are used (this is the default).
  • Option Compare: Set to Binary or Text. This specifies if strings are compared using binary or text comparison operations.
  • Option Strict: Set to On or Off. Off is the default. When you assign a value of one type to a variable of another type Visual Basic will consider that an error if this option is on and there is any possibility of data loss, as when you're trying to assign the value in a variable to a variable of less precise data storage capacity.

Example

Option Strict Off
Module Module1
    Sub Main()
        System.Console.WriteLine("Hello from Visual Basic")
    End Sub

End Module

Imports Statements

You use Imports statements to import a namespace so you don't have to qualify items in that namespace by listing the entire namespace when you refer to them.

Example
The WriteLine procedure is built into the System.Console namespace, so it is a method of that namespace, and to use it, I qualify its name with the namespace it belongs to:

Option Strict Off
Module Module1

    Sub Main()
        System.Console.WriteLine("Hello from Visual Basic")
    End Sub

End Module

On the other hand, if we import the System.Console namespace, that makes that namespace immediately available, so we don't have to qualify the WriteLine method name anymore (note that Option statements, if there are any, must still come first):

Option Strict Off
Imports System.Console
Module Module1

    Sub Main()
        WriteLine("Hello from Visual Basic")
    End Sub

End Module

Declaring Variables

You need to store some data in your program—so you need to declare some variables. In VB.NET you must declare all variables before using them by default. You can do that with the Dim statement.

Example

Dim EmployeeID As Integer = 1
Dim EmployeeName As String = "Bob Owens"
Dim EmployeeAddress As String

To create a new object, you use the New keyword.

Examples

Creating a new Module

The following example is a Visual Basic module, and modules are designed to hold code. Sub Main indicates the entry point of our program—the part that will be executed first. And we're using the WriteLine method of the System.Console class to write to the console (that is, DOS window). Methods are procedures that are built into classes—note the syntax here; to call a method, you specify the class or object the method is part of, followed by a dot (.) and the method name. This tells Visual Basic where to look to find the method you want to use.

Module Module1

    Sub Main()
        System.Console.WriteLine("Hello from Visual Basic")

    End Sub

End Module

IF statement

IF statement starts with if(BankBalance < 0) Then and ends with End If, creating a block of statements.

Module Module1

    Sub Main()
        Dim BankBalance As Single = 500.01
        If (BankBalance < 0) Then
            Dim strError As String
            strError = "Hey, your bank balance is negative!"
            System.Console.WriteLine(strError)
        End If
    End Sub

End Module

Constants

The following example creates a constant named Pi, as well as Area and Radius variables, using the * (multiplication) operator to find the area of a circle, then converting that area from a number to a string with the Visual Basic Str function, and displaying the result.

Imports System.Console
Module Module1

    Sub Main()
        Const Pi = 3.14159
        Dim Radius, Area As Single
        Radius = 1
        Area = Pi * Radius * Radius
        WriteLine("Area = " & Str(Area))
    End Sub

End Module
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.