Microsoft.NET

……………………………………………….Expertise in .NET Technologies

VB.NET Option Explicit and VB.NET Option Strict

Posted by Ravi Varma Thumati on June 17, 2010

VB.NET Option Explicit [On | Off]

Option Explicit

statement ensures whether the compiler requires all variables to be explicitly declared or not before it use in the program.

Option Explicit [On Off]

The

Option Explicit has two modes. On and Off mode. If Option Explicit mode in ON , you have to declare all the variable before you use it in the program . If not , it will generate a compile-time error whenever a variable that has not been declared is encountered .If the Option Explicit mode is OFF , Vb.Net automatically create a variable whenever it sees a variable without proper declaration.

By default the

Option Explicit is On

With the Option Explicit On , you can reduce the possible errors that result from misspelled variable names. Because in Option Explicit On mode you have to declare each variable in the program for storing data.

Take a look at the following programs, it will give you a clear picture of Option Explicit.

The following program is a normal vb.net program , so the default mode of Option Explicit On is using. The default is Option Explicit On , so we do not need to put it in the source code.

VB.NET Source Code

  Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles Button1.Click

        Dim someVariable As String

        someVariable = “Option Explicit ON”

        MsgBox(someVariable)

    End Sub

 

  End Class

The above program , declare a String variable and assigned a value in it and it displays.

Take a look at the following program , it is an example of

Option Explicit Of

Option Explicit Off

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles Button1.Click

        someVariable = “Option Explicit ON”

        MsgBox(someVariable)

    End Sub

End Class
Here

“someVariable” is not declared , because the Option Explicit Of , without any compiler error you can continue the program.

VB.NET Option Strict [On | Off]

Option Strict

is prevents program from automatic variable conversions, that is implicit data type conversions .

Option Strict [On Off]

By default

Option Strict is Off

From the following example you can understand the use of Option Strict.

VB.NET Source Code

  Public Class Form1

               Private Sub Button1_Click(ByVal sender As System.Object, _

               ByVal e As System.EventArgs) Handles Button1.Click

                               Dim longNum As Long

                               Dim intNum As Integer

                               longNum = 12345

                               intNum = longNum

                               MsgBox(intNum)

               End Sub

 

  End Class

The above program is a normal vb.net program and is in default

Option Strict Off . Because its Option Strict Off we can convert the value of Long to an Integer.

Take a look at the following program.

Option Strict On

Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim longNum As Long Dim intNum As Integer longNum = 12345 intNum = longNum MsgBox(intNum) End Sub End Class

When you write this source code the compiler will shows the message 

“Option Strict On disallows implicit conversions from ‘Long’ to ‘Integer'” 

The compiler generate error because in the program we put “Option Strict On” and prevent the program from automatic conversion.

Advertisements

3 Responses to “VB.NET Option Explicit and VB.NET Option Strict”

  1. pinumallaprashant said

    Excellent blog you made it.
    Thanks a lot.

  2. Ken said

    Very Good Explanation.
    Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: