MELJUN CORTES VB.net_Visual Basic .NET Language

Embed Size (px)

Citation preview

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    1/64

    Visual Basic .NET Programming

    Visual Basic .NET 

    Language

    Data Types, Variables and Arrays

    Operators, Conditionals, and Loops

    Procedures (Sub and Function)

    Visual Basic .NET Language * Property of STI Page 1 of 64

    Exception Handling

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    2/64

    Visual Basic .NET Programming

    Data

    Types

    The data type of a variable or constant

    indicates what type of information will be

    stored in the allocated memory space.

    Each type has

    a name (e.g., Integer) and

    a size e. . 4 b tes

    Visual Basic .NET Language * Property of STI Page 2 of 64

     

    • size indicates the number of bytes each

    object of this type occupies in memory

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    3/64

    Visual Basic .NET Programming

    TypeSize (in

    bytes)Description

    Boolean 2 True or false.

    Byte 1 Unsigned (values 0-255).

    Char 2Unicode characters (0-65,535

    unsigned)

    Date 8Midnight 1/1/0001 through

    Data

    Types

    Visual Basic .NET Language * Property of STI Page 3 of 64

      .

    Decimal 16

    Fixed-precision numbers up to 28digits and the position of the

    decimal point; typically used infinancial calculations; requiresthe suffix "m" or "M."

    Double 8

    Double-precision floating-pointnumbers; holds the valuesfrom approximately +/-5.0 *10-324 to approximately +/-1.8* 10308 with 15-16 significantfigures.

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    4/64

    Visual Basic .NET Programming

    Data

    Types

    TypeSize (in

    bytes)Description

    Integer 4

    Integer values between -

    2,147,483,648 and2,147,483,647.

    Long 8

    Integers ranging from -9,223,372,036,854,775,808to

    Visual Basic .NET Language * Property of STI Page 4 of 64

    9,223,372,036,854,775,807.

    Short 2 Integer values -32,768 to 32,767.

    Single 4

    Floating-point numbers; holdsthe values fromapproximately +/-1.5 * 10-45

    to approximate +/-3.4 * 1038

    with 7 significant figures.

    String VariesA sequence of Unicode

    characters.

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    5/64

    Visual Basic .NET Programming

    Data Types

    Changes

    Visual Basic

    6.0Visual Basic .NET

    Integer Short

    Long Integer

    (none) Long

    Visual Basic .NET Language * Property of STI Page 5 of 64

    Changes to Existing Data Types

     

    Variant Not supported: use Object

    Currency Not supported: use Decimal

    Date No longer stored as a Double

    String (fixed

    length)Not supported

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    6/64

    Visual Basic .NET Programming

    CType

    Use the CType function to convert any

    value from one data type to another.

    Similar to CStr and CInt in VB 6.0.

    Syntax:

    CType (expression, typename)

    Visual Basic .NET Language * Property of STI Page 6 of 64

    Example:

    Dim strX as String, intY as

    Integer

    strX=“34”

    intY=CType(strX, Integer)

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    7/64

    Visual Basic .NET Programming

    Variables

    A variable is an object that can hold a

    value.

    A variable is a named storage location

    (i.e., stored in memory) with a type.

    Declaring and Initializing Variable

    Visual Basic .NET Language * Property of STI Page 7 of 64

    yn ax:

    Dim Identifier [As DataType] =value/expression

    Example:

    Dim i As Integer = 15

    Dim dToday As Date = Now()

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    8/64

    Visual Basic .NET Programming

    Variable

    Defaults

    If variables are not initialized, it will

    have the default value based on the

    type of variable assigned as type.

    Datatype Default value

    Visual Basic .NET Language * Property of STI Page 8 of 64

    Boolean FALSE

    Date 01/01/0001 12:00:00 AM

    Object Nothing

    String Nothing

    Default values for uninitialized variables

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    9/64

    Visual Basic .NET Programming

    Variable

    Scope

    Variable scope can be declared as

    Private

    Public

    Visual Basic .NET Language * Property of STI Page 9 of 64

    Shared

    Protected

    Friend

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    10/64

    Visual Basic .NET Programming

    Constants

    A constant is like a variable that can store

    a value, but it cannot be changed while

    the program runs.

    Declaring Constant

     

    Visual Basic .NET Language * Property of STI Page 10 of 64

      =

    expression

    Example:

    Const consX as Integer = 100

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    11/64

    Visual Basic .NET Programming

     Arrays

    Array is a memory location that is used to

    store multiple values.

    An array is an indexed collection of

    objects, all of the same type (e.g., all

    integers, all strings, etc.).

    Visual Basic .NET Language * Property of STI Page 11 of 64

    It is the simplest collection of objects in

    VB.NET.

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    12/64

    Visual Basic .NET Programming

    Types of

     Arrays One-dimensional arrays

    with element identified by a single

    subscript

    Multidimensional arrays

    allows you to create rows of elements, oneabove the other

    Visual Basic .NET Language * Property of STI Page 12 of 64

    • Rectangular

    – all the rows are of the same

    length

    • Jagged

    – each row has different length

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    13/64

    Visual Basic .NET Programming

    Declaring

    an Array 

    Declaring one-dimensional array

    Dim Array_Name (Num_Elements)

    [As Element_Type]

    where

     

    Visual Basic .NET Language * Property of STI Page 13 of 64

    Array_Name – name of the array

    Num_Elements – number of elements the

    array can containElement_Type – data type of elements

    Example:

    Dim aryEmp_Name(100) as String

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    14/64

    Visual Basic .NET Programming

    Declaring

    an Array 

    Declaring multidimensional array

    Dim Array_Name ( , ) [As

    Element_Type]

    A two-dimensional array has two indices.

    Visual Basic .NET Language * Property of STI Page 14 of 64

    The number of dimension in an array is

    called the rank of an array.

    Example:

    Dim arr(10,2) as String

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    15/64

    Visual Basic .NET Programming

    Dynamic

     Arrays

    The size of a dynamic array can vary

    during the execution of the program.

    Creating dynamic array

    use Dim statement, declaring it with empty

    Visual Basic .NET Language * Property of STI Page 15 of 64

    Dim arr_Name() as String

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    16/64

    Visual Basic .NET Programming

    ReDim

    Statement 

    Use ReDim statement to specify or change

    the size of one or more dimensions of an

    array that has already been declared.

    Syntax:

     

    Visual Basic .NET Language * Property of STI Page 16 of 64

      _ 

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    17/64

    Visual Basic .NET Programming

    Preserve

    Keyword 

    Use the Preserve keyword with the ReDim

    statement. It copies the elements of the

    old array to the new one before modifying

    the dimension of the array.

    Dim arr_Name() as String

    ReDim Preserve arr_Name(100)

    Visual Basic .NET Language * Property of STI Page 17 of 64

     

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    18/64

    Visual Basic .NET Programming

    Visual Basic

    Operators

    The arithmetic operators are as follows:

    ^ Exponentiation

    * Multiplication

    / Division

     \ Integer division

    Visual Basic .NET Language * Property of STI Page 18 of 64

    Mod Modulus

    + Addition

    - Subtraction

    >> right shift on a bit pattern

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    19/64

    Visual Basic .NET Programming

    Visual Basic

    Operators

    The assignment operators are as follows:

    = Assignment

    ^= Exponentiation followed by

    assignment

    *= Multiplication followed by

    Visual Basic .NET Language * Property of STI Page 19 of 64

    /= Division followed by

    assignment  \= Integer division followed by

    assignment

    += Addition followed by

    assignment

    -= Subtraction followed by

    assignment &= String concatenation

    followed by assignment

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    20/64

    Visual Basic .NET Programming

    Visual Basic

    Operators

    The comparison operators are as follows:

    < Less than

    Greater than

    >= Greater than or equal to

    =

    Visual Basic .NET Language * Property of STI Page 20 of 64

     

    Not equal to

    Is True if two object references

    refer to the same object

    Like Performs string pattern

    matching

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    21/64

    Visual Basic .NET Programming

    Visual Basic

    Operators

    The string concatenation operators are as

    follows:

    & String concatenation

    + String concatenation

    Visual Basic .NET Language * Property of STI Page 21 of 64

     

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    22/64

    Visual Basic .NET Programming

    Visual Basic

    Operators

    The logical/bitwise operators are as

    follows:

    And Performs an “And” operation

    Not Reverses the logical value of

    its operand, from true to false

    and false to true.

    Visual Basic .NET Language * Property of STI Page 22 of 64

    Or Performs an “Or” operation.

    Xor Performs an “Exclusive Or”

    operation. AndAlso A “short-circuited” And

    operator.

    OrElse A “short-circuited” Or operator.

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    23/64

    Visual Basic .NET Programming

    Visual Basic

    Operators

    Other operators are as follows:

    AddressOf Gets the address of a

    procedure

     

    Visual Basic .NET Language * Property of STI Page 23 of 64

    GetType Gets information

    about a type

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    24/64

    Visual Basic .NET Programming

    Visual Basic

    Operators Operator Precedence

    predetermined order to which the

    operations occurred in an expression at

    once is evaluated

    Arithmetic and Concatenation

    • Ex onentiation ^

    Visual Basic .NET Language * Property of STI Page 24 of 64

     

    • Negation (-)

    • Multiplication and division (*,/)

    • Integer division (\)• Modulus arithmetic (Mod)

    • Addition and subtraction (+,-)

    • String concatenation (+)

    • String concatenation (&)

    • Arithmetic bit shift ()

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    25/64

    Visual Basic .NET Programming

    Visual Basic

    Operators Operator Precedence (con’t)

    Comparison

    • Equality (=)

    • Inequality ()

    • Less than, greater than ()

    • Greater than or equal to (>=)

    • Less than or equal to (

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    26/64

    Visual Basic .NET Programming

    Visual Basic

    Operators Operator Precedence Example:

    Dim intScore1, intScore2,

    intScore3, _ 

    intNumberStudents As Integer

    intScore1 = 50

    intScore2 = 60

     

    Visual Basic .NET Language * Property of STI Page 26 of 64

     

    intNumberStudents = 3

    Console.WriteLine(“Average

    grade = “ & _ 

    intScore1 + intScore2 +

    intScore3 / intNumberStudents)

    Console.WriteLine (“Press

    enter to continue…”)

    Console.ReadLine ()

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    27/64

    Visual Basic .NET Programming

    Conditional

    Statements also known as branching statements

    allows a programmer to make decisions

    and take different paths in the code

    If statement

     

    Visual Basic .NET Language * Property of STI Page 27 of 64

    Se ect statement

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    28/64

    Visual Basic .NET Programming

     If

    Statements

    Making Decisions with If Statements

    If condition Then

    [statements]

    ElseIf condition-1 Then

    [statements-1]

    Visual Basic .NET Language * Property of STI Page 28 of 64

    .

    .

    .

    ElseIf condition-n Then

    [statements-n]

    Else

    [else statements]

    End If

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    29/64

    Visual Basic .NET Programming

     If

    Statements

    Example:

    Dim intQtyOrdered as Integer = 10

    Dim dblDiscount as Double = 0

    If intQtyOrdered

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    30/64

    Visual Basic .NET Programming

    Select

    Statements

    Making Decisions with Select Statements

    Select Case test-expression

    Case expression-1

    [statement-1]

    Case expression-2

    -

    Visual Basic .NET Language * Property of STI Page 30 of 64

    .

    .

    .

    Case expression-n

    [statement-n]

    Case Else

    [else-statement]

    End Select

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    31/64

    Visual Basic .NET Programming

    Select

    Statements Example:

    Dim strMonth as String

    strMonth = InputBox(“Type name of

    the month”)Select Case strMonth

    Case “January”

    MessageBox.Show(“Month of

    Visual Basic .NET Language * Property of STI Page 31 of 64

    JANUARY”)

    Case “February”

    MessageBox.Show(“Month ofFEBRUARY”)

    .

    .

    Case “December”

    MessageBox.Show(“Month of

    DECEMBER”)Case Else

    MessageBox.Show(“Incorrect

    Month”)

    End If

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    32/64

    Visual Basic .NET Programming

    Control 

    Statements also known as looping

    repeat action(s) for a number of times or

    until a specified condition is reached

    For/Next statement

     

    Visual Basic .NET Language * Property of STI Page 32 of 64

    For Each/Next statement

    Do/Loop statement

    While/End statement

    With statement

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    33/64

    Visual Basic .NET Programming

    For…Next

    Statements

    Using the For…Next Loop

    For index [As dataType] = start To

    end 

    [statements]

    [Exit For]

    [statements]

    Visual Basic .NET Language * Property of STI Page 33 of 64

    Next [index ]

    Example:

    For intCtr As Integer = 0 To 10

    MessageBox.Show(“Counter is “ &

    intCtr)

    Next intCtr

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    34/64

    Visual Basic .NET Programming

    For Each…Next

    Statements

    Using the For Each…Next Loop

    For Each element [As dataType] In

    group

    [statements]

    [Exit For]

    [statements]

    Visual Basic .NET Language * Property of STI Page 34 of 64

    Next [element]

    Example:

    Dim arrDays() as String =

    (“Mon”,”Tue”,”Wed”,”Thu”,”Fri”)

    For Each strElem As String in

    arrDays

    MessageBox.Show(strElem)

    Next

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    35/64

    Visual Basic .NET Programming

    Do…Loop

    Statements

    Using the Do…Loop

    Do [While | Until] condition

    [statement]

    Exit Do

    [statements]

    Visual Basic .NET Language * Property of STI Page 35 of 64

    Or 

    Do

    [statement]

    Exit Do

    [statements]

    Loop [While | Until] condition

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    36/64

    Visual Basic .NET Programming

    Do…Loop

    Statements

    Example:

    Dim intCtr as Integer = 1

    Do While intCtr

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    37/64

    Visual Basic .NET Programming

    While…End While

    Statements

    Using the While Loop

    While condition

    [statement]

    End While

    Visual Basic .NET Language * Property of STI Page 37 of 64

    Dim intCtr as Integer = 1While intCtr

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    38/64

    Visual Basic .NET Programming

    With

    Statements

    Using the With Statement

    With object

    [statements]

    End With

    Visual Basic .NET Language * Property of STI Page 38 of 64

    With TextBox1.Height = 50

    .Width = 500

    .Text = “Enter value here”

    End With

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    39/64

    Visual Basic .NET Programming

    Procedures

    contain all of the executable code in the

    application

    gives the ability to logically group code

    that will perform certain task

    Visual Basic .NET Language * Property of STI Page 39 of 64

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    40/64

    Visual Basic .NET Programming

    Procedure

    access modifiers

    Access Modifiers determine the scope

    within which a procedure can be called

    Public

    Private

    Visual Basic .NET Language * Property of STI Page 40 of 64

    Shared

    Protected

    Friend

    Protected Friend

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    41/64

    Visual Basic .NET Programming

     Advantages of

    Procedures

    code in a procedure is reusable

    application is easier to debug and

    maintain

    Visual Basic .NET Language * Property of STI Page 41 of 64

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    42/64

    Visual Basic .NET Programming

    Types of

    Procedures

    Sub Procedure

    performs specific tasks

    does not return a value to a calling

    statement

    Function Procedure

     

    Visual Basic .NET Language * Property of STI Page 42 of 64

     

    returns a value to a calling statement

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    43/64

    Visual Basic .NET Programming

    Sub

    Procedure

    The syntax for creating a Sub procedure

    is:

    [ ] [ {Overloads |Overrides | Overridable |

    NotOverridable | MustOverride |

    Shadows | Shared }]

     

    Visual Basic .NET Language * Property of STI Page 43 of 64

     Pu c | Protecte | Fr en |

    Protected Friend | Private }]

    Sub SubName [(arglist)]

    [statements]

    [Exit Sub]

    [statements]

    End Sub

    The syntax for calling a Sub procedure is:

    [ Call ]

    ([Arguments list])

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    44/64

    Visual Basic .NET Programming

    Sub

    Procedure

    Example:

    Public Sub ComputeTotalAmount(ByVal

    dblQty _ As Double, ByValdblPrice As Double)

    Dim dblAmount As Double

    dblAmount = dblQty * dblPrice

    Visual Basic .NET Language * Property of STI Page 44 of 64

    Console.Writeline(dblAmount)

    End Sub

    ‘calling the sub procedure

    Sub Main()

    Call ComputeTotalAmount(10, 50)

    End Sub

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    45/64

    Visual Basic .NET Programming

    Function

    Procedures

    The syntax for creating a Function

    procedure is:

    [][{Overloads | Overrides

    | Overridable |NotOverridable | MustOverride |

    Shadows | Shared }]

    [{ Public | Protected | Friend |

    Visual Basic .NET Language * Property of STI Page 45 of 64

    ro ec e r en r va e

    Function functionName [(arglist)]

    [As type]

    [statements]

    [Exit Function]

    [statements]

    End Function

    The syntax for calling a Function

    procedure is:

    ReturnValue =

    ([Arguments list])

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    46/64

    Visual Basic .NET Programming

    Function

    Procedure

    Example:

    Public Function

    GetTotalAmount(ByVal dblQty

    As Double, ByVal dblPrice As

    Double) As Double

    Dim dblAmount As Double

    Visual Basic .NET Language * Property of STI Page 46 of 64

    dblAmount = dblQty * dblPrice

    Console.Writeline(dblAmount)

    Return dblAmountEnd Sub

    ‘calling the function procedure

    Sub Main()

    Dim dblAmount As Double

    dblAmount = GetTotalAmount (10,50)

    End Sub

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    47/64

    Visual Basic .NET Programming

    Built-in

    Functions

    The following are the built-in functions in

    Microsoft.VisualBasic Namespace:

    Microsoft.VisualBasic.Conversion

    ErrorToString

     

    Visual Basic .NET Language * Property of STI Page 47 of 64

     

    Hex

    Oct

    Str

    Val

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    48/64

    Visual Basic .NET Programming

    Built-in

    Functions

    Microsoft.VisualBasic.DateAndTime

    DateAdd

    DateDiff 

    DatePart

    DateSerial

    Visual Basic .NET Language * Property of STI Page 48 of 64

    DateValue

    Day

    Hour

    Minute

    Month

    MonthName

    Now

    Second

    TimeOfDay

    Timer

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    49/64

    Visual Basic .NET Programming

    Built-in

    Functions

    Microsoft.VisualBasic.DateAndTime

    TimeSerial

    TimeString

    TimeValue

    Today

    Visual Basic .NET Language * Property of STI Page 49 of 64

    WeekDayName

    Year

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    50/64

    Visual Basic .NET Programming

    Built-in

    Functions

    Microsoft.VisualBasic.String

    ASC

    Chr

    Filter

    FormatNumber, FormatCurrency and

    FormatPercent

    Visual Basic .NET Language * Property of STI Page 50 of 64

    FormatDateTime

    GetChar

    InStr

    InStrRev

    Join

    LCase

    Left

    Len

    LSet, RSet

    LTrim,Trim,RTrim

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    51/64

    Visual Basic .NET Programming

    Built-in

    Functions

    Microsoft.VisualBasic.String

    Mid

    Replace

    Space

    Split

    Visual Basic .NET Language * Property of STI Page 51 of 64

    StrConv

    StrReverse

    UCase

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    52/64

    Visual Basic .NET Programming

    Exception

    Handling

    Visual Basic .NET has good support in

    handling runtime errors or exceptions

    ways of Handling Exceptions

    Unstructured – centers on the

    Visual Basic .NET Language * Property of STI Page 52 of 64

    n rror o o s a emen

    Structured – centers on the Try/Catchstatement

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    53/64

    Visual Basic .NET Programming

    Types of

    Errors

    Syntax Errors

    due to misspelled keywords or variables

    Logic Errors

    code does not act expected

     

    Visual Basic .NET Language * Property of STI Page 53 of 64

    Runtime Errors

    due to unhandled unexpected error

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    54/64

    Visual Basic .NET Programming

    Unstructured

    Exception Handling

    Unstructured exception handling revolves

    around the

    On Error GoTo statement

    On Error {GoTo [ line |0|-1]|

    Resume Next }

    Visual Basic .NET Language * Property of STI Page 54 of 64

    GoTo line – calls the error-handling

    codes that starts at the line specified at

    line

    GoTo 0 – disables the enabled error

    handler in the current procedure

    GoTo -1 – disables the enabled error

    handler in the current procedure

    Resume Next – specifies that when an

    exception occurs, execution skips over thestatement that caused the problem and

    goes to the statement immediately

    following

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    55/64

    Visual Basic .NET Programming

    Unstructured

    Exception Handling

    Example:

    Private Sub GetName()

    On ErrorGoTo errHandler

    [code]

    Exit Sub

    Visual Basic .NET Language * Property of STI Page 55 of 64

    ‘Handle error

    End Sub

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    56/64

    Visual Basic .NET Programming

    Exception’s Number

    and Description

    a built in error object named Err has a

    Number property that determines an

    error’s number

    when testing the program, useErr.Number to determine the number of

    errors that ma occur and use those

    Visual Basic .NET Language * Property of STI Page 56 of 64

     

    numbers to handle errors in different ways

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    57/64

    Visual Basic .NET Programming

    Resume

    Statement 

    resumes program execution in

    unstructured exception handling

    Syntax:

    Resume [0]

    Resume Next

     

    Visual Basic .NET Language * Property of STI Page 57 of 64

     

    Resume resumes execution with thestatement that caused the error.

    Resume Next resumes execution with the

    statement after the one that caused the

    error

    Resume line resumes execution at line,

    a line number or label that specifies where

    to resume execution

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    58/64

    Visual Basic .NET Programming

    Resume

    Statement 

    Example:

    Private Sub DeleteAndCopyFile()

    On Error Resume Next

    If Dir(“C:\file.txt”)

    “ “ Then

    Kill(“C:\file.txt”)

    Visual Basic .NET Language * Property of STI Page 58 of 64

    If Err.Number 0 Then

    Err.Clear()

    End If

    FileCopy(“E:\File.txt”,”

    C:\file.txt”)

    If Err.Number 0 Then

    MsgBox(“Cannot Copy File,

    Please Try Again”)

    End IfEnd Sub

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    59/64

    Visual Basic .NET Programming

    Structured

    Exception Handling

    centers on the Try/Catch statement

    advantages

    supported by multiple languages

    allows to create protective blocks of code

     

    Visual Basic .NET Language * Property of STI Page 59 of 64

    a ows er ng o excep ons s m ar oSelect Case statement

    allows nested handling

    code is easier to read, debug and maintain

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    60/64

    Visual Basic .NET Programming

    Try…Catch…

    Finally 

    Syntax:

    Try

    [ tryStatements ]

    [Catch [ exception1 [ As type1 ] ]

    [ When expression1 ]

    catchStatements1

     

    Visual Basic .NET Language * Property of STI Page 60 of 64

     

    [Catch [ exception2 [ As type2 ] ]

    [ When expression2 ]

    catchStatements2

    [Exit Try]

    .

    .

    [Catch [ exceptionn [ As typen ] ]

    [ When expressionn ]

    catchStatementsn

    [Exit Try]

    [Finally

    [finally Statements] ]

    End Try

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    61/64

    Visual Basic .NET Programming

    Try…Catch…

    Finally 

    Parts of the syntax are:

    Try – starts the try block

     tryStatements – specifies the sensitive

    statements where possible exceptions are

    anticipated

    Catch – starts the block that handles the

    Visual Basic .NET Language * Property of STI Page 61 of 64

    exception(s)

    exception – specifies the variable given

    to the exception type – indicates the type of the exception

    wanted to catch in a Catch block

    When expression – specifies a Catch

    block clause that means the Catch block

    will catch exceptions only when expression

    is True

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    62/64

    Visual Basic .NET Programming

    Try…Catch…

    Finally 

    Parts of the syntax are:

     catchStatements – specifies statements

    that handle exceptions occurring in the Tryblock

    Exit Try – exists a Try/Catch statement

    immediately.

     

    Visual Basic .NET Language * Property of STI Page 62 of 64

    Finally – starts a Finally block that is

    always executed when execution leaves

    the Try/Catch statement finallyStatements - specifies

    statements that are executed after all

    other exception processing has occurred

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    63/64

    Visual Basic .NET Programming

    Try…Catch…

    Finally 

    Example:

    Sub TrySimpleException

    Dim i1, i2, iResult As Decimali1 = 21

    i2 = 0

    Try

     

    Visual Basic .NET Language * Property of STI Page 63 of 64

    Result = 1 2 Cause

    divide-by-zero error

    MsgBox (iResilt) ‘ Will not

    execute

    Catch eException As Exception

    ‘ Catch the exception

    MsgBox (eException.Message)

    ‘ Will not execute

    Finally

    Beep

    End Try

    End Sub

  • 8/8/2019 MELJUN CORTES VB.net_Visual Basic .NET Language

    64/64

    Visual Basic .NET Programming

    System.Exception

    Class

    Property ormethod

    Information provided

    Message property why the exception was

    thrown

    provides information about a particular

    exception

    Source property the name of the application

    or object that generated the

    exceptionStackTraceproperty

    exception history

    InnerExceptionproperty

    for nested exceptions

    HelpLink property the appropriate Help file,

    URN, or URLToString method the name of the exception