ARTICLE

Option Statement in VB.NET

Posted by Hirendra Sisodiya Articles | Visual Basic 2010 April 23, 2010
This article gives a brief description of the Option Statement available in VB.NET.
 
Reader Level:

The Option Statement

There is one  statement that is very important to know when making an application- Option statement

There are three option statements, these are as follows:

(a) Option Explicit:

    It has two modes:

     1.  On

     2.  Off

 Any VB.net program has 'On' mode of option explicit by default. If program has 'Option explicit On' statement than it requires all variables have proper deceleration otherwise it gives compile time error and another  mode is 'Off', if we use 'Option explicit Off' statement  than  vb.net  create variable declaration automatically and program does not give an error

We can take an example for better understanding:

 

Option Explicit On

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        TempString = "Hello"

        MessageBox.Show(TempString)

    End Sub

End Class

Above program gives an error 'Name TempString is not declared'

 

 

Option Explicit Off

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        TempString = "Hello"

        MessageBox.Show(TempString)

    End Sub

End Class

 

Above program run continue without error

 

Note: For better coding it is recommended to declare variables with Dim keyword and data type.

 

 

(b) Option Compare:

 

This statement also has two modes

1.       Binary

2.       Text

 

Option Compare is Binary by default; we can change string comparison method by set the text or Binary see example:

 

 

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If "Hello" = "HELLO" Then

            MessageBox.Show("True")

        Else

            MessageBox.Show("False")

        End If

    End Sub

End Class

 

In above program if we use Option Compare Binary,messagebox show 'false' and if we use 'Text' mode than messagebox show 'True.  that means when we set Option Compare to Text we can able compare string with Case insensitive comparision.

(c)  Option Strict:

    It has also two modes:

     1.  On

     2.  Off

Option Strict Off is the default mode. 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.

Option Strict On

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim tempInt As Integer = 2010

        MessageBox.Show(tempInt)

    End Sub

End Class

Above program we can not use messagebox.show mehtod like this, it is need to change 'tempInt'  integer  type value to string type just like that:

MessageBox.Show(CStr(tempInt))

I think this is enough description about option statement…thanks

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor