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