Sei sulla pagina 1di 23

Chapter 1

Programming in VB.net / Chapter 1 / 1 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

Programming in VB.net / Chapter 1 / 2 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 provides background compilation The only language in VS.net that supports late binding Not case-sensitive like C, C++, or Java
Programming in VB.net / Chapter 1 / 3 of 24

Data types in VB.net


Category Data type Length(in bits)

Numeric

Integer Long

32 64 128 8 16
Programming in VB.net / Chapter 1 / 4 of 24

Decimal Character

Decimal Byte Char

Data types (Cont)


Category Data type Length(in bits)

String
Object

String
Object

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

An Example
Dim d1 As Double Dim d2 As Object Dim d3, d4, d5 As Integer Dim mystring As String Dim a As Integer = 5 d1 = 12 d2 = 10.5 d3 = 4 mystring = Jonathan
Programming in VB.net / Chapter 1 / 6 of 24

Declaring Constants
Syntax to declare a constant is as follows:

[Public | Private | Friend | Protected |Prot ected Friend] Const constantname [As type] = expression For example: Public Const DaysInYear = 365 Private Const WorkDays = 250
Programming in VB.net / Chapter 1 / 7 of 24

Programming Constructs
Decision statements VB.net provides 3 decision statements: If ..then If..then..else Select..Case

If-th en

If-th -e en lse
Programming in VB.net / Chapter 1 / 8 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 Else Return 0 End If

Programming in VB.net / Chapter 1 / 9 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 100 Case Else ' KRA is < 1 or > 14. Return 0 End Select

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

Programming Constructs
Loop structures supported by VB.net are: While Do..Loop For..Next For Each..Next

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

Example for Loops


While Loop
Dim Cnt As Integer = 0 Dim Nbr As Integer = 10

Do..Loop
Dim Cnt As Integer =0 Dim Nbr As Integer =10 Do While Nbr > 6 Nbr = Nbr 1

While Nbr > 6


Nbr = Nbr - 1 Cnt = Cnt + 1 End While

Cnt = Cnt + 1 Loop

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

Example for Loops -11


For..Next
Dim Num1,Total As Integer For Num1= 1 To 5 Step 1 Total = Total + Num1 Next Num1

For Each..Next
Dim X As Integer For Each X In A

X = 128
Next X

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

Arrays
Arrays are objects in VB.net. An array can be of any fundamental data type, a structure, or an object class. Array variables can have the Public, Protected, Friend, Private, or Protected Friend specifier.
100 250 640 Cynthia 710 Patric k

Programming in VB.net / Chapter 1 / 14 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

Single dimensional array

The above code declares the array variable Expenditure, initializes it to have 366 elements, and then assigns an initial value of 20 to each element.
Programming in VB.net / Chapter 1 / 15 of 24

Example for arrays - 11


Dim EmpData(3) EmpData(0) = EmpData(1) = EmpData(2) = EmpData(3) = As Object "astalavista" "42 Heaven Towers" 100 Format("09-10-1992", "Date")

Mixing data types in an array is possible if it is declared of type Object. The following example stores employee information in the array variable EmployeeData.
Programming in VB.net / Chapter 1 / 16 of 24

Multidimensional Arrays
Dim Num1, Num2 As Integer Dim MDim0, MDim1 As Integer Dim MatA(10, 10) As Double MDim0 = MatA.GetLength(0) 1 MDim1 = MatA.GetLength(1) - 1 For Num1 = 0 To MDim0 For Num2 = 0 To MDim1 MatA(Num1, Num2) = (Num1 * 10) + Num2 Next Num2 Next Num1

Creates Multidimensional array

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

Redim Statement
ReDim Preserve MyArray(5, 20)

The above statement allocates a new array, initializes its elements from the corresponding elements of the existing MyArray, and assigns the new array to MyArray.

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

Object-oriented Features
Abstraction Polymorphism

Object-oriented language

Encapsulation

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

Declaring Classes

A class is declared with the Class..End Class declarations. An Example:

Public Class TestClass End Class

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

Option Statement Variations


Option statement Value Option Explicit On Off Option Compare Binary Description Requires declaration of all the variables before they are used Variables can be used without declaring them (Default) Strings are compared using text algorithm

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

Option Statement Variations-11


Option statement Option Strict Value Description

On Off

Automatic type correction will not take place Automatic type correction will take place (Default)

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

Implementing Typecasting
Typecasting in VB.net is implemented by means of the Type() statement.
For example:

Dim Num1 As Long Dim Num2 As Double Num1 = 500 Num2 = CType(Num1,Double)
Programming in VB.net / Chapter 1 / 23 of 24

Potrebbero piacerti anche