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 now we talk about another mode ‘Off’ – if we are using variables without declaration 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 with out 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
- Binary
- 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))