Programming Fundamentals
Programming Fundamentals
VB .NET Programming
Contents
Arrays?
Using Arrays
Using Classes
Events and Event Handlers
Exceptions and Exception Handling
Coding Conventions
Objectives
At the end of this module you should be able to:
Define an array
Describe stacks
Describe queues
What is an Array?
An array is a data structure that
contains a number of variables,
which are accessed through
computed indices.
The variables contained in an array,
are called the elements of the array.
They must all be of the same type,
and this type is called the element
type of the array.
Dim numbers() as Integer
A one-dimensional
array on the heap
Values
The
heap
Identifier
name
Indexing
operator
type
= new int(4)
4
Creating an Array
There are three steps to create an array:
1. Declaration
2. Construction / Creation
3. Initialization
Manipulating Arrays
The following are methods for
manipulating arrays:
Sample Output:
100
200
300
6
6
System.Console.WriteLine(sum)
End Sub
End Class
Single-Dimensional Arrays
Single-Dimensional Arrays
Are declared as
Note: You can also omit the new operator from the above example. You can
assign these values directly without using the new operator.
dim myStringArray as string()=
{Sun,Sat,Mon,Tue,wed,Fri};
8
Multi-Dimensional Arrays
Multi-Dimensional Arrays
Are declared as
Dim numbers as
integer(,)
numbers = new
integer(4,2)
Two-Dimensional Arrays
Classes
A blueprint of an object
A class acts as the template from which an instance of an object is
created. The class defines the properties of the object and the
methods used to control the object's behavior.
VB.Net uses classes to do specific tasks
E.g. Console
10
11
Class
Variables
Methods
Properties
Events
12
Class Members
13
Class Modifiers
Modifiers change the way a class can be used
Access modifiers control the accessibility of a class
Modifier
public
Description
protected
private
Friend
noninheritable
Must inherit
protected Friend
14
Member Modifiers
Modifier
Description
Impact on Members
Shadows
public
protected
protected
internal
internal
private
Description
Impact on Members
MustInhe
rit
Shared
readonly
Field, Property
NotInher
atable
WriteOnl
y
Property
16
Description
Impact on Member
overrides
17
Shared Members
The Keyword Shared is used when the member of the class is
common among all instances of the class.
Public Class Dog
Protected _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Sub Bark()
Console.Write("Arf! Arf!")
End Sub
Public Shared _species As String
End Class
Output:
18
What is a Method?
A method is a portion of code referring to behaviors associated
either with an object or its class
It is used to access and process data contained in the object
It is also used to provide responses to any messages received from other
objects
It is the executable code that implements the logic of a particular
message for a class
19
Method Types
There are 2 types of Methods
Sub
Functions
Function SubtractNumbers(ByVal x As
Integer, ByVal y As Integer) As
Integer
dim difference as integer = x-y
return difference
End Function
Returns a value
Syntax:
[accessor] Function [Name] (parameters) as ReturnType
method_body
End Function
20
Creating a Method
Method declaration consists of five components:
21
Creating a Method
Method signature
consists of the method name and its
parameters
must be unique for each method in a class
return statement
allows the method to return a value to its
caller
also means to stop the execution of the
current method and return to its caller
implicit return at the end of the method
Class Number
Private Function multiply(ByVal i As
Integer, ByVal j As Integer) As
Integer
Return i * j
End Function
Private Function divide(ByVal i As
Integer, ByVal j As Integer) As
Integer
Return i / j
End Function
Private Sub printSum(ByVal i As
Integer, ByVal j As Integer)
Console.WriteLine(i + j)
End Sub
22
23
Method Calling
Public Class CSharpMain
Public Shared Sub Main(ByVal args As
String())
' create a Person object
Dim you As New Person()
you.talk()
you.jump(3)
Console.WriteLine(you.tellAge())
Class Person
Public Sub talk()
Console.WriteLine("blah, blah...")
End Sub
blah, blah...
whoop!
whoop!
whoop!
I'm 10
blah, blah...
whoop!
whoop!
24
Method Types
VB.Net employs four kinds of parameters that are passed to methods
Value Type parameters
25
Reference Parameters
A variable of a reference type
does not contain its data directly
Imports System
Class PassingValByRef
Private Shared Sub SwapByRef(ByRef x As
Integer, ByRef y As Integer)
Dim temp As Integer = x
x = y
y = temp
End Sub
Public Shared Sub Main()
Dim i As Integer = 2
Dim j As Integer = 3
Console.WriteLine("i = {0} j = {1}", i,
j)
SwapByRef(i, j)
Console.WriteLine("i = {0} j = {1}", i,
j)
End Sub
End Class
i=2j=3
i=3j=2
27
Parameter Arrays
The paramarray keyword allows you to specify a method parameter
that takes an argument where the number of arguments is variable.
28
Parameter Arrays
Public Class [MyClass]
Public Shared Sub UseParams(ByVal
ParamArray list As Integer())
For i As Integer = 0 To list.Length 1
Console.WriteLine(list(i))
Next
Console.WriteLine()
End Sub
Public Shared Sub UseParams2(ByVal
ParamArray list As Object())
For i As Integer = 0 To list.Length 1
Console.WriteLine(list(i))
Next
Console.WriteLine()
End Sub
End Class
1
2
3
1
a
test
10
11
12
13
29
Properties
Properties are special types of methods.
There are 2 methods for a given property
A Getter
Key Points
An array is a collection of values (either primitive or objects) all of which
have the same data type
Key Points
Return statement returns a value to its caller or returns control to its
caller
A method that does not return a value must specify void as a return
type
Calls to a method should match its method signature
When calling a method in the same class, use only the method name
(Nested Methods)
When calling a method outside class, use the object reference
When calling a shared method, use the class name
In vb.NEt method parameters are passed using pass by value, pass
by reference and parameter arrays
Properties are always public and serves as the how external classes
will access the variables inside a class
32