Showing posts with label error. Show all posts
Showing posts with label error. Show all posts

Sunday, 22 July 2012

Option Strict [On Off] in vb.net

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.

Download Source CodePrint Source Code
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.

Option Explicit [On Off] in vb.net

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

Download Source CodePrint Source Code
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.

On Error GoTo in vb.net

On Error GoTo statements is an example of Vb.Net's Unstructured Exception Handling . VB.NET has two types of Exception handling . Structured Error Handling and Unstructured Error handling . VB.NET using Try..Catch statement for Structured Error handling and On Error GoTo statement is using for Unstructured Error handling.

Error GoTo redirect the flow of the program in a given location.

On Error Resume Next - whenever an error occurred in runtime , skip the statement and continue execution on following statements.

Take a look at the following program

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 result As Integer
        Dim num As Integer
        num = 100
        result = num / 0
        MsgBox("here")
    End Sub
  End Class

when u execute this program you will get error message like " Arithmetic operation resulted in an overflow "

See the program we put an On Error GoTo statement

Download Source CodePrint Source Code
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        On Error GoTo nextstep
        Dim result As Integer
        Dim num As Integer
        num = 100
        result = num / 0
nextstep:
        MsgBox("Control Here")
    End Sub
End Class

When you execute the program you will get the message box "Control Here" . Because the On Error statement redirect the exception to the Label statement.

Implicit Type Conversions & Explicit Type Conversions in vb.net

Implicit Type Conversions

Implicit Conversion perform automatically in VB.NET, that is the compiler is taking care of the conversion.

The following example you can see how it happen.

1. Dim iDbl As Double

2. Dim iInt As Integer

3. iDbl = 9.123

4. MsgBox("The value of iDbl is " iDbl)

5. iInt = iDbl

6. MsgBox("The value of iInt is " iInt)

line no 1 : Declare a Double datatype variable iDble

line no 2 : Declare an Integer datatyoe variable iInt

line no 3 : Assign a decimal value to iDbl

line no 4 : Display the value of iDbl

line no 5 : Assign the value of iDbl to iInt

line no 6 : Display the value of iInt

The first messagebox display the value of iDbl is 9.123

The second messegebox display the value od iInt is 9

iInt display only 9 because the value is narrowed to 9 to fit in an Integer variable.

Here the Compiler made the conversion for us. These type fo conversions are called Implicit Conversion .

The Implicit Conversion perform only when the Option Strict switch is OFF

Download Source CodePrint Source Code
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Dim iDbl As Double
        Dim iInt As Integer
        iDbl = 9.123
        MsgBox("The value of iDbl is " & iDbl)
        iInt = iDbl
        'after conversion
        MsgBox("The value of iInt is " & iInt)
    End Sub
End Class

Explicit Type Conversions
In some cases we have to perform conversions , that is the compiler does not automatically convert a type to another . These type of conversion is called Explicit conversion . An explicit conversion uses a type conversion keyword. With these conversion keywords we hav to perform the Explicit Conversion.

try catch block in vb.net

Exceptions are the occurrence of some condition that changes the normal flow of execution (simply when you get some errors or your program is crashed for that you can show he errors by using try... catch block). For ex: your program is run out of memory, file does not exist in the given path, network connections are dropped etc. More specifically for better understanding, we can say it as Runtime Errors.

In .NET languages, Structured Exceptions handling is a fundamental part of Common Language Runtime. It has a number of advantages over the On Error statements provided in previous versions of Visual Basic. All exceptions in the Common Language Runtime are derived from a single base class, also you can create your own custom Exception classes. You can create an Exception class that inherits from Exception class.

You can handle Exceptions using Try..Catch statement .

  Try
                code
                exit from Try
  Catch [Exception [As Type]]
                code - if the exception occurred this code will execute
                exit from Catch

Finally

The code in the finally block will execute even if there is no Exceptions. That means if you write a finally block , the code should execute after the execution of try block or catch block.

  Try
                code
                exit from Try
  Catch [Exception [As Type]]
                code - if the exception occurred this code will execute
                exit Catch
  Finally
                code - this code should execute , if exception occurred or not

From the following VB.NET code , you can understand how to use try..catch statements. Here we are going to divide a number by zero .

Download Source CodePrint Source Code
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim i As Integer
            Dim resultValue As Integer
            i = 100
            resultValue = i / 0
            MsgBox("The result is " & resultValue)
        Catch ex As Exception
            MsgBox("Exception catch here ..")
        Finally
            MsgBox("Finally block executed ")
        End Try
    End Sub
End Class

When you execute this program you will "Exception catch here .." first and then "Finally block executed " . That is when you execute this code , an exception happen and it will go to catch block and then it will go to finally block.