Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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
Numeric
Integer Long
32 64 128 8 16
Programming in VB.net / Chapter 1 / 4 of 24
Decimal Character
String
Object
String
Object
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
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 Constructs
Loop structures supported by VB.net are: While Do..Loop For..Next For Each..Next
Do..Loop
Dim Cnt As Integer =0 Dim Nbr As Integer =10 Do While Nbr > 6 Nbr = Nbr 1
For Each..Next
Dim X As Integer For Each X In A
X = 128
Next X
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
Creates
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
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
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.
Object-oriented Features
Abstraction Polymorphism
Object-oriented language
Encapsulation
Inheritance
Programming in VB.net / Chapter 1 / 19 of 24
Declaring Classes
On Off
Automatic type correction will not take place Automatic type correction will take place (Default)
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