[go: up one dir, main page]

0% found this document useful (0 votes)
11 views22 pages

PWVB-LecturesNotesNo 10

This document contains lecture notes for a course on Programming with Visual Basic at SUMAIT University, focusing on arrays, functions, and object-oriented programming concepts. It covers the creation, initialization, and usage of arrays, as well as the definition and implementation of functions in VB.NET. Additionally, it introduces object-oriented programming principles, including classes, objects, data hiding, and inheritance, with practical examples throughout.

Uploaded by

mussathobias4791
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views22 pages

PWVB-LecturesNotesNo 10

This document contains lecture notes for a course on Programming with Visual Basic at SUMAIT University, focusing on arrays, functions, and object-oriented programming concepts. It covers the creation, initialization, and usage of arrays, as well as the definition and implementation of functions in VB.NET. Additionally, it introduces object-oriented programming principles, including classes, objects, data hiding, and inheritance, with practical examples throughout.

Uploaded by

mussathobias4791
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

SUMAIT UNVERSITY

Lectures Notes No. 10

Programming with Visual Basic


BSc. (IT)
Year: II, Semester: II
Couse Code: CS364

Dr. Antar S.H. Abdul-Qawy

Department of Mathematics
and Computer Science

SUMAIT University, Zanzibar, Tanzania


Programming with Visual Basic
CS364, IT, 2nd Year

Dr. Antar Abdul-Qawy, Senior Lecturer


Department of Math & Computer Science
Abdulrahman Al-Sumait University, Zanzibar
antarabdulqawy@sumait.ac.tz

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Lecture Note No. 10

Arrays & Functions

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

1
Agenda
▪ Arrays in VB .NET ▪ Example 5, 6, 7: Using Class and Objects
▪ Creating Arrays in VB .NET ▪ Inheritance in VB.NET
▪ Initializing Arrays in VB .NET ▪ Visual Illustration of Inheritance
▪ Example 1: Integer arrays ▪ General form of inheritance
▪ Example 2: String arrays ▪ Example 8 ,9, 10: Implementation of Inheritance
▪ Functions in VB .NET ▪ Types of Inheritance in VB.NET
▪ Function Return Methods in VB .NET ▪ Visual Illustration of Single Inheritance
▪ Example 3: Return by Using Return Statement ▪ Example 11: Implementation of Single Inheritance
▪ Example 4: Return by Assigning to Function Name ▪ Visual Illustration of Multilevel Inheritance
▪ Introduction to OOP in VB .NET ▪ Example 12: Implementation of Multilevel Inheritance
▪ Fundamentals of OOP: Classes and Objects ▪ Visual Illustration of Hierarchical Inheritance
▪ OOP: Data Hiding and Methods ▪ Example 13: Implementation of Hierarchical Inheritance
▪ Visual Illustration of OPP ▪ Interfaces in VB.NET
▪ Advantages of OOP ▪ Example 14, 15: Implementation of Interface
▪ Defining a Class in Visual Basic ▪ References

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Arrays

Arrays in VB .NET • An array stores a fixed-size sequential


collection of elements of the same type
under one variable name.

• All arrays occupy contiguous memory


locations, ensuring elements are stored one
after another in sequence.

• The lowest memory address corresponds to


the first element of the array, while the
highest address corresponds to the last
element.

• Arrays facilitate efficient indexed access to


elements starting from index 0, common in
VB .NET and many programming languages.

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

2
Arrays

Creating Arrays in VB .NET ▪ Use the Dim statement to declare arrays in


VB .NET, similar to declaring other variables

'an array of 31 elements


Dim intData(30) As Integer

'an array of 21 strings


Dim strData(20) As String

'a two dimensional array of integers


Dim twoDarray(10, 20) As Integer

'a two dimensional array as string


Dim twoDarray(10, 20) As String

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Arrays

Initializing Arrays in VB .NET ▪ You can also initialize the array elements while
declaring the array

▪ The elements in an array can be stored and


accessed by using the index of the array which
starts with 0.

Dim intData() As Integer = {12, 16, 20}

Dim names() As String = {"ali", "sami”}

Dim byteData() As Char = {"A", "B","C"}

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

3
Arrays

Example 1: Integer arrays

Example 1: use an Module Module1


integer array Sub Main()
Dim n(10) As Integer
Dim i, j As Integer
This example For i = 0 To 10
explains how to use a n(i) = i + 100 ' set array elements
single integer array: Next i
initialize and print For j = 0 To 10
the elements, staring Console.WriteLine("Element({0}) = {1}", j, n(j))
with n(0) = 100 Next j
Console.ReadKey()
End Sub
End Module

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Arrays

Example 2: String arrays

Example 2: use a Module Module1


string array Sub Main()
Dim n(10) As String
Dim i, j As Integer
This example For i = 0 To 10
explains how to use a n(i) = "Student No." & i 'Element of location i
single string array: Next i
initialize and print the For j = 0 To 10
elements, staring Console.WriteLine("Element({0}) = {1}", j, n(j))
with n(0) = “Student Next j
No.0”
Console.ReadKey()
End Sub
End Module

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

4
Functions

Functions in VB .NET
▪ A Function is a type of procedure that returns a value to the
calling code after execution, unlike Subs which do not return
values.
▪ Functions are declared using the Function statement,
specifying the function name, parameters (optional), and
return type.
▪ The syntax for defining a function is:

Function FunctionName (ParameterList) As ReturnType

Statements . . .

End Function

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Functions

Function Return Methods in VB .NET

Using Return Statement Assigning to Function Name


▪ The Return statement sends the ▪ In VB.NET, you can assign the
specified value back to the calling return value to the function's
code immediately. name within the body.
▪ This is a more explicit and widely ▪ This method is unique to VB.NET
used method for returning values and differs from languages like C#
in VB.NET. or Java.
▪ Using Return can improve code ▪ The assigned value will be
readability and prevent accidental returned when the function ends.
errors by ending function
execution.

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

5
Arrays

Example 3: Return by Using Return Statement

Example 3: Return Module Module1


by using return Sub Main()
statement Dim a As Integer = 100
Dim b As Integer = 200
Dim res As Integer
res = FindMax(a, b)
This example Console.WriteLine("Max value is : {0}", res)
Console.ReadLine()
explains how to use End Sub
function with Return Function FindMax(num1 As Integer, num2 As Integer) As Integer
by using return Dim result As Integer
If (num1 > num2) Then
statement result = num1
Else
result = num2
End If
Return (result)
End Function
End Module

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Arrays

Example 4: Return by Assigning to Function Name

Module Module1
Example 4: Return Sub Main()
by Assigning to Dim a As Integer = 100
Function Name Dim b As Integer = 200
Dim res As Integer
res = FindMax(a, b)
Console.WriteLine("Max value is : {0}", res)
This example Console.ReadLine()
explains how to use End Sub
Function FindMax(num1 As Integer, num2 As Integer) As Integer
function with return Dim result As Integer
by assigning to If (num1 > num2) Then
function name result = num1
Else
result = num2
End If
FindMax = result
End Function
End Module

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

6
Object Oriented
Programming in VB
.NET

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Object-Oriented Programming

Introduction to OOP in VB .NET


Understanding OOP vs Procedural Programming

▪ The arrangement of separate data and functions in


structured programming is not a good modeling of
things and objects in real-world (becomes unreal-
world modeling).

▪ In real-world, objects have two things: Attributes


(equivalent to data in a program, e.g., blue for eye
color, & 4 for number of doors) and Behavior
(equivalent to functions, actions that can be
performed, e.g., display inventory).

▪ OOP combines data and behavior into objects,


providing a more natural way to model real-world
entities compared to procedural programming.

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

7
Object-Oriented Programming

Fundamentals of OOP: Classes and ▪ Class is a blueprint that defines and


Objects describes what the thing we are talking about
is (e.g., student, car, building).

▪ Class defines attributes (properties) and


behavior (methods) of the things.

▪ Object is created from the idea of class;


multiple objects can be created each
differing in its properties such as name, age,
address.

▪ Attributes represent the data or properties of


the object, and methods define the
behaviors or functions that operate on those
attributes.

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Object-Oriented Programming

OOP: Data Hiding and Methods


Data Hiding and Member Functions

▪ An object’s functions, called member functions,


typically provide the only way to access its data.

▪ The data is hidden; direct access to data is not


allowed.

▪ To read or modify data in an object, you call a


member function (method) that accesses the data
and returns or changes the value.

▪ This encapsulation protects the data from


unintended interference and misuse.

▪ Member functions (methods) define the object's


behaviors and interact with its hidden data,
maintaining integrity and security.

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

8
Object-Oriented Programming

Visual
Illustration of
OPP

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Object-Oriented Programming

Visual
Illustration of
OPP

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

9
Object-Oriented Programming

Class Box
Private a as Integer
Public x As String
Visual Public func() As String
.
Illustration of .
OPP End Class

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Object-Oriented Programming

Advantages of OOP
Real-World Enables the use of real-world modeling (e.g., a car
Modeling is an object that has attributes, such as an
engine, wheels, etc).

Code Reuse Promotes the reuse of code (the code used to


design a Toyota car can be used for designing
Suzuki cars).

Flexibility Provides flexibility in the modification of an


existing application (the new car model would be
able to share or reuse some of the other models’
code).

Data Security Maintains security through the use of data hiding


and abstraction mechanism, where only
necessary data can be viewed.

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

10
Object-Oriented Programming

Defining a Class in Visual Basic


▪ The class keyword is used to define a class followed by the class
name.
▪ The class body contains declarations of attributes and methods.
▪ Use End Class to mark the end of the class definition.

Class Student
Dim Name As String = "Ahmed" 'String data
Dim Age As Integer = 30 'Integer data
Public Sub showInfo() 'a function
Console.WriteLine("Name is: {0}", Name)
Console.WriteLine("Age is: {0}", Age)
End Sub
End Class

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Object-Oriented Programming

Example 5: Using Class and Objects

Example 5: Using Module Module1


class and objects Class myClass
Public x As Integer
Public y As Integer
This is a simple End Class
Sub Main()
example that explains
Dim obj1 As New myClass()
how to use class and
obj1.x = 10
objects with public obj1.y = 20
data Dim z As Integer = obj1.x + obj1.y
Console.WriteLine("Sum is equal to: {0}", z)
Console.ReadKey()
End Sub
End Module

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

11
Object-Oriented Programming

Example 6: Using Class and Objects

Example 6: Using Module Module1


class and objects Class Box
Public length As Double
Public width As Double
Public height As Double
This is another End Class
example that explains Sub Main()
how to use class and Dim Box1 As New Box()
objects with public Dim volume As Double = 0.0
data Box1.height = 5.0
Box1.length = 6.0
Box1. width = 7.0
volume = Box1.height * Box1.length * Box1. width
Console.WriteLine("Volume of Box1 : {0}", volume)
Console.ReadKey()
End Sub
End Module

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Object-Oriented Programming

Example 7: Using Class and Objects

Example 7 Using Module Module1


class and objects Class Student
Dim Name As String = "Ahmed"
Dim Age As Integer = 30
This is another Public Sub showInfo() 'a function
Console.WriteLine("Name is: {0}", Name)
example that explains
Console.WriteLine("Age is: {0}", Age)
how to use class and
End Sub
objects with a public End Class
function Sub Main()
Dim S1 As New Student()
S1.showInfo()
Console.ReadKey()
End Sub
End Module

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

12
Inheritance in OOP

Inheritance in VB.NET
▪ Inheritance is the ability of a class to
inherit/derive properties and characteristics
of another class.

▪ It allows a hierarchy of classes to be built,


moving from most general to most specific

▪ A base/super class (parent) defines common


qualities inherited by other classes
(subclasses).

▪ A derived/sub class (child) inherits features


from the base class and adds its specific
qualities.

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Inheritance in OOP

Visual
Illustration of
Inheritance

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

13
Inheritance in OOP

General form of inheritance


▪ When you write a class-based program, it is important to identify
the class from which the derived class is being derived, that is, its
base class.

▪ The general form of inheritance syntax in VB.NET is as shown


below.

'derived_class base_class
Class myclass2 : Inherits myclass1
'Members of derived class here
End Class

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Inheritance in OOP

Example 8: Implementation of Inheritance


Example 8: Module Module1
Implementation of Class myclass1
Inheritance Public x As Integer
End Class
Class myclass2 : Inherits myclass1
This is a simple Public y As Integer
example that explains End Class
how to use Sub Main()
inheritance with data Dim obj As New myclass2()
obj.x = 10
only
obj.y = 15
Dim z = obj.x + obj.y
Console.WriteLine("The sum is: {0}", z)
Console.ReadKey()
End Sub
End Module

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

14
Inheritance in OOP

Example 9: Implementation of Inheritance


Example 9: Module Module1
Implementation of Class myclass1
Inheritance Public myId As Integer
End Class
Class myclass2 : Inherits myclass1
This is another Public Sub showId()
example that explains Console.WriteLine("MyId is {0}", myId)
how to use End Sub
inheritance with data End Class
Sub Main()
accessed by a
Dim newObj As New myclass2()
function
newObj.myId = 80
newObj.showId()
Console.ReadKey()
End Sub
End Module

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Inheritance in OOP

Example 10: Implementation of Inheritance


Example 10: Module Module1
Implementation of Class Animal
Inheritance Public Sub baseSub()
Console.WriteLine(" Animal class")
End Sub
End Class
This is another
Class Dog : Inherits Animal
example that explains Public Sub childSub()
how to use Console.WriteLine("Dog class")
inheritance with a End Sub
End Class
public function
Sub Main()
Dim childDog As New Dog()
childDog.baseSub()
childDog.childSub()
Console.ReadKey()
End Sub
End Module

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

15
Inheritance in OOP

Types of Inheritance in VB.NET

Single Inheritance Multilevel Inheritance Hierarchical Inheritance Interface

Derived class inherits from Derived class inherits from a More than one derived Defines method declarations
only one base class. The class which itself is derived classes inherit from a single without implementation.
child class has a single from another class, forming base class, creating a Allows multiple inheritance
parent class. a chain of inheritance. hierarchy of child classes by implementing multiple
from one parent. interfaces.

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Inheritance in OOP
▪ Single Inheritance happens when derived class is allowed to
inherit from only one base class (the child class has only one
parent class).

Visual
Illustration of
Single
Inheritance

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

16
Inheritance in OOP

Example 11: Implementation of Single Inheritance


Example 11: Module Module1
Implementation of Class A
single inheritance Public Name As String
End Class
Class B : Inherits A
This a simple Public ID As Integer
example that explains End Class
Sub Main()
how to Implement
Dim b1 As New B()
single Inheritance b1.Name = "Juma"
b1.ID = 102
Console.WriteLine("The name is: {0}", b1.Name)
Console.WriteLine("The Id is: {0}", b1.ID)
Console.ReadKey()
End Sub
End Module

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Inheritance in OOP
▪ Multilevel Inheritance happens when a derived class inherits all
its properties from a class that itself inherits from another class
(a child class has a parent that itself a child for another class).

Visual
Illustration of
Multilevel
Inheritance

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

17
Inheritance in OOP

Example 12: Implementation of Multilevel Inheritance


Example 12: Module Module1
Implementation of Class A
Public Name As String
multilevel End Class
Inheritance Class B : Inherits A
Public ID As Integer
End Class
Class C : Inherits B
This a simple Public job As String
End Class
example that explains
Sub Main()
how to Implement Dim c1 As New C()
multilevel Inheritance c1.Name = "Juma"
c1.ID = 102
c1.job = "teacher"
Console.WriteLine("The name is: {0}", c1.Name)
Console.WriteLine("The Id is: {0}", c1.ID)
Console.WriteLine("The job is: {0}", c1. job)
Console.ReadKey()
End Sub
End Module

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Inheritance in OOP
▪ Hierarchical Inheritance: happens when multiple derived classes
inherit their properties from just a single base class (multiple
child classes has the same parent classes).

Visual
Illustration of
Hierarchical
Inheritance

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

18
Inheritance in OOP

Example 13: Implementation of Hierarchical Inheritance


Example 13: Module Module1
Implementation of Class A
Public Name As String
hierarchical End Class
Inheritance Class B : Inherits A
Public ID As Integer
End Class
Class C : Inherits A
Public ID As String
This a simple End Class
Sub Main()
example that explains Dim b1 As New B()
how to Implement Dim c1 As New C()
b1.Name = "Juma"
hierarchical b1.ID = 102
c1.Name = "Saleh"
Inheritance c1.ID = 115
Console.WriteLine("First name is: {0}", b1.Name)
Console.WriteLine("First Id is: {0}", b1.ID)
Console.WriteLine("Second name is: {0}", c1.Name)
Console.WriteLine("Second Id is: {0}", c1.ID)
Console.ReadKey()
End Sub
End Module

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Inheritance in OOP

Interfaces in VB.NET

Definition of Interface Multiple Inheritance


Interface is the same as a class but it contains only the Multiple inheritance can be achieved by using Interface in
declarations of methods (functions), properties, and VB.NET, overcoming class inheritance limitations.
events without implementation.

Important Rules Class Implementation


✓ Keyword 'Interface' is used instead of 'Class’. A derived class must implement all base interface's
member functions using the 'Implements' keyword to
✓ A class can inherit from one or more base interfaces.
provide method definitions.

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

19
Inheritance in OOP

Example 14: Implementation of Interface


Example 14: Module module1
Implementation of Interface A
interface Sub showMsg()
End Interface
Class mClass : Implements A
This a simple Public Sub myShowMsg() Implements A.showMsg
example that explains Console.WriteLine("This is main interface")
how to Implement End Sub
interface End Class
Sub Main()
Dim obj As New mClass
obj.myShowMsg()
Console.ReadKey()
End Sub
End Module

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

Inheritance in OOP

Example 15: Implementation of Interface


Example 15: Module module1
Implementation of Interface inter1
Sub hello1()
interface End Interface
Interface inter2
Sub hello2()
End Interface
This is another Public Class mClass : Implements inter1, inter2
Public Sub myHello1() Implements inter1.hello1
example that explains Console.WriteLine("This is first interface")
how to Implement End Sub
Public Sub myHello2() Implements inter2.hello2
interface Console.WriteLine("This is second interface")
End Sub
End Class
Sub Main()
Dim obj As New mClass
obj.myHello1()
obj.myHello2()
Console.ReadKey()
End Sub
End Module

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

20
References

References

▪ Object Oriented Programming in VB.Net, Alistair McMonnies

▪ Accelerated VB 2008, Fouché and Nash

▪ Microsoft Visual Studio Documentation -


https://docs.microsoft.com/en-us/visualstudio/

▪ https://www.tutorialspoint.com/vb.net/vb.net_loops.htm

▪ Internet

Dr. Antar Abdul-Qawy. Abdulrahman Al-Sumait University, Zanzibar, antarabdulqawy@sumait.ac.tz

21

You might also like