Edited Session1

Embed Size (px)

Citation preview

  • 8/2/2019 Edited Session1

    1/23

    Programming in VB.net / Chapter 1 / 1 of 24

    Chapter 1

  • 8/2/2019 Edited Session1

    2/23

    Programming in VB.net / Chapter 1 / 2 of 24

    Objectives

    Identify VB.net as part of .Net framework

    Declare variables and constants

    Discuss programming constructs

    Implement arrays

    Discuss OO features of VB.net

    Implement typecasting

  • 8/2/2019 Edited Session1

    3/23

    Programming in VB.net / Chapter 1 / 3 of 24

    Features of VB.net

    VB.net is:

    A part of Visual Studio.net

    A full-fledged object-oriented language

    The only language in VS.net that providesbackground compilation

    The only language in VS.net that supportslate binding

    Not case-sensitive like C, C++, or Java

  • 8/2/2019 Edited Session1

    4/23

    Programming in VB.net / Chapter 1 / 4 of 24

    Data types in VB.net

    Category Data type Length(in bits)

    Numeric Integer 32

    Long 64

    Decimal Decimal 128

    Character Byte 8

    Char 16

  • 8/2/2019 Edited Session1

    5/23

    Programming in VB.net / Chapter 1 / 5 of 24

    Data types (Cont)

    Category Data type Length(in bits)

    String String -

    Object Object -

  • 8/2/2019 Edited Session1

    6/23

    Programming in VB.net / Chapter 1 / 6 of 24

    An Example

    Dim d1 As Double

    Dim d2 As Object

    Dim d3, d4, d5 As IntegerDim mystring As String

    Dim a As Integer = 5

    d1 = 12

    d2 = 10.5

    d3 = 4

    mystring = Jonathan

  • 8/2/2019 Edited Session1

    7/23

    Programming in VB.net / Chapter 1 / 7 of 24

    Declaring Constants

    Syntax to declare a constant is as follows:

    [Public | Private | Friend | Protected |Prot

    ected Friend]Constconstantname [As type]= expression

    For example:

    Public Const DaysInYear = 365

    Private Const WorkDays = 250

  • 8/2/2019 Edited Session1

    8/23

    Programming in VB.net / Chapter 1 / 8 of 24

    Programming Constructs

    Decision statements

    VB.net provides 3 decision statements:If ..then

    If..then..else

    Select..Case

    If-then-elseIf-then

  • 8/2/2019 Edited Session1

    9/23

    Programming in VB.net / Chapter 1 / 9 of 24

    An Example(1)

    Dim KRA As Decimal

    If KRA = 1 Then

    Return Bonus * 0.1

    ElseIf KRA = 2 Then

    Return Bonus * 0.09

    ElseIf KRA = 3 Then

    Return Bonus * 0.07

    ElseReturn 0

    End If

  • 8/2/2019 Edited Session1

    10/23

    Programming in VB.net / Chapter 1 / 10 of 24

    An Example(2)

    Dim KRA As Decimal

    Select Case KRA

    Case 1 ' KRA is 1.

    Return Bonus * 0.1

    Case 2, 3 ' KRA is 2 or 3.Return Bonus * 0.09

    Case 5 To 7 ' KRA is 5, 6, or 7.

    Return Bonus * 0.07

    Case 4, 8 To 10 ' KRA is 4, 8, 9, or 10.

    Return Bonus * 0.05

    Case Is < 15 ' KRA is 11, 12, 13, or 14.

    Return 100Case Else ' KRA is < 1 or > 14.

    Return 0

    End Select

  • 8/2/2019 Edited Session1

    11/23

    Programming in VB.net / Chapter 1 / 11 of 24

    Programming Constructs

    Loop structures supported by VB.net are:

    While

    Do..Loop

    For..Next

    For Each..Next

  • 8/2/2019 Edited Session1

    12/23

    Programming in VB.net / Chapter 1 / 12 of 24

    Example for Loops

    Dim Cnt As Integer = 0

    Dim Nbr As Integer = 10

    While Nbr > 6

    Nbr = Nbr - 1

    Cnt = Cnt + 1

    End While

    Dim Cnt As Integer =0

    Dim Nbr As Integer =10

    Do While Nbr > 6

    Nbr = Nbr 1

    Cnt = Cnt + 1

    Loop

    While Loop Do..Loop

  • 8/2/2019 Edited Session1

    13/23

    Programming in VB.net / Chapter 1 / 13 of 24

    Example for Loops -11

    Dim Num1,Total AsInteger

    For Num1= 1 To 5 Step 1

    Total = Total + Num1

    Next Num1

    Dim X As Integer

    For Each X In A

    X = 128

    Next X

    For..Next For Each..Next

  • 8/2/2019 Edited Session1

    14/23

    Programming in VB.net / Chapter 1 / 14 of 24

    Arrays

    Arrays are objects in VB.net.

    An array can be of any fundamental datatype, a structure, or an object class.

    Array variables can have the Public,

    Protected, Friend, Private, or ProtectedFriend specifier.

    100

    250

    640

    710Cynthia

    Patrick

  • 8/2/2019 Edited Session1

    15/23

    Programming in VB.net / Chapter 1 / 15 of 24

    Example for Arrays

    Dim Expenditure(366) As Decimal

    Dim I As Integer

    For I = 0 to 365

    Expenditure(I) = 20

    Next I

    Creates

    Singledimensionalarray

    The above code declares the array variable Expenditure, initializes it tohave 366 elements, and then assigns an initial value of 20 to eachelement.

  • 8/2/2019 Edited Session1

    16/23

    Programming in VB.net / Chapter 1 / 16 of 24

    Example for arrays - 11

    Dim EmpData(3) As Object

    EmpData(0) = "astalavista"

    EmpData(1) = "42 Heaven Towers"

    EmpData(2) = 100

    EmpData(3) = Format("09-10-1992", "Date")

    Mixing data types in an array is possible if it is declared of typeObject. The following example stores employee information in thearray variable EmployeeData.

  • 8/2/2019 Edited Session1

    17/23

    Programming in VB.net / Chapter 1 / 17 of 24

    Multidimensional Arrays

    Dim Num1, Num2 As IntegerDim MDim0, MDim1 As IntegerDim MatA(10, 10) As Double

    MDim0 = MatA.GetLength(0) 1MDim1 = MatA.GetLength(1) - 1

    For Num1 = 0 To MDim0For Num2 = 0 To MDim1MatA(Num1, Num2) = (Num1 * 10) + Num2

    Next Num2Next Num1

    CreatesMulti-dimensionalarray

  • 8/2/2019 Edited Session1

    18/23

    Programming in VB.net / Chapter 1 / 18 of 24

    Redim Statement

    ReDim Preserve MyArray(5, 20)

    The above statement allocates a new array, initializes itselements from the corresponding elements of the existing

    MyArray, and assigns the new array to MyArray.

  • 8/2/2019 Edited Session1

    19/23

    Programming in VB.net / Chapter 1 / 19 of 24

    Object-oriented Features

    Object-oriented language

    Abstraction Polymorphism

    Encapsulation Inheritance

  • 8/2/2019 Edited Session1

    20/23

    Programming in VB.net / Chapter 1 / 20 of 24

    Declaring Classes

    A class is declared with the Class..End Class declarations.

    An Example:

    Public Class TestClass

    End Class

  • 8/2/2019 Edited Session1

    21/23

    Programming in VB.net / Chapter 1 / 21 of 24

    Option Statement Variations

    Option statement Value Description

    Option Explicit On Requires declaration of all thevariables before they are used

    Off Variables can be used withoutdeclaring them (Default)

    Option Compare Binary Strings are compared using textalgorithm

  • 8/2/2019 Edited Session1

    22/23

    Programming in VB.net / Chapter 1 / 22 of 24

    Option Statement Variations-11

    Optionstatement

    Value Description

    Option Strict On Automatic type correction will nottake place

    Off Automatic type correction will takeplace (Default)

  • 8/2/2019 Edited Session1

    23/23

    Programming in VB.net / Chapter 1 / 23 of 24

    Implementing Typecasting

    Typecasting in VB.net is implemented by means ofthe Type() statement.

    For example:

    Dim Num1 As Long

    Dim Num2 As Double

    Num1 = 500

    Num2 = CType(Num1,Double)