Professional Documents
Culture Documents
IT202 OOP
Using VB .Net
Lecturer:
F.Masimba
Bsc (Hons) information Systems
Msc Information Systems.
PhD Student Information systems, Vaal University of Technology(SA)
Object Oriented Programming
(OOP)
Outlines
What is Object-Oriented Programming ?
Procedural vs. Object-Oriented Programming
OO Programming Concepts
Concept of Objects and classes
UML Class Diagram
Visibility Modifiers and Accessor Methods
Full Example
Let’s think about the concept of Class
and Object
Classes & Objects !
What is Class ?
Data Fields:
radius is _______
Methods:
getArea
Class & Object
Class & Object
Class & Object
Class & Object
Thinking to build class …
}
Class Members
Reserved word
Course Details
Assignments & Presentations
2 assignments all typed and submitted through google
classroom or via my email: bmukhalela@cuz.ac.zw
1: Group Presentation
1. user interface
2. data access
3. database connectivity
4. cryptography
5. web application development
6. numeric algorithms
7. network communications
It must not contain any embedded space or symbol like ? - +! @ # % ^ &
* ( ) [ ] { } . ; : " ' / and \. However, an underscore ( _ ) can be used.
number1 – Identifier
Integer – Data Type
Dim – Reserved / Keyword
Variable Initialization in VB .NET
Variables are initialized (assigned a value) with an
equal sign followed by a constant expression
Example:
Dim name as String
name = “John”
Exercise
Write a simple program that declares variables number1
and number2, and performs the arithmetic operations
below
Lecturer:
Braiton U Mukhalela BSc CIS (AU) Hon BScIS
36
(UNISA),MComIS(GZU).
Constant Variables
Constants refer to fixed values that the program
may not alter during execution.
The fixed values are also called Literals
Constants can be of any data type but remain
fixed throughout the execution of a program
Example:
Const PI as Double = 3.14
Enumerations
An enumeration is a set of named integer constants
A Variable that declared with specific values allocated to it.
An enumerated type is declared using the Enum statement.
The Enum statement declares an enumeration and defines the
values of its members.
The Enum statement can be used at the module, class,
structure, procedure, or block level.
Example
Enum Colors
red = 0
orange = 1
yellow = 2
End Enum
Console.WriteLine("The Color Red is : " & Colors.red)
More on Operators
Arithmetic: +, -, /, *, ^ , \ , Mod
Comparison:
=, > , <, >= , <= , <>
Example:
For a = 10 To 20
Console.WriteLine("value of a: {0}", a)
Next
You can also specify the number of steps
While….End
Example:
While a < 20
Console.WriteLine("value of a:
{0}", a)
a=a+1
End While
Nested Loops
For counter1 [ As datatype1 ] = start1 To end1 [ Step
step1 ]
For counter2 [ As datatype2 ] = start2 To end2
[ Step step2 ]
<statements>
Next [ counter2 ]
Next [ counter 1]
Loop Control – Exit and Continue
Exit
Terminates the loop or select case statement and transfers
execution to the statement immediately following the loop
or select case.
Continue
Causes the loop to skip the remainder of its body and
immediately retest its condition prior to reiterating.
Group Presentation Question 1
Write a Visual Basic Program that prompts a user to
enter a character, length and width of a box. For
example *, Length – 4 and width 4 should give result:
****
****
****
****
Group Presentation Question 2
Write a Visual Basic Program that allows a
user to enter any positive integer for an
infinite number of times and terminates
execution by entering a negative value. After
execution the program should print the sum,
average, maximum and minimum of the
values entered.
Group Presentation Question 3
Write a visual basic program that accepts temperature in degrees
Celsius and converts it to Fahrenheit given that
Creating a string
Example:
Dim myString as String = “Artwell”
Properties and methods of the
string class
String.compare(String A, String B) – Integer (0) True
String.concat(String A, String B) – String
.length – Integer
.contains(String A) – Boolean
IndexOf(String A) – Integer
toCharArray() – Character Array
ToLower() – lowercase
toUpper() – Uppercase
.substring(Integer) - String
String Concatenation
Dim name = “Akim”
Dim surname = “Ndlovu”
Dim result = name & “ ” & lastname
To declare an array:
Dim myArr(10) as Integer
‘This declares an array of 10 Elements
Note: There is a difference between the array size and the upper
bound
Array Initialization
Arrays can be initialized statically or
dynamically through the use of a loop
Example:
Dim intData() As Integer = {12, 16, 20, 24,
28, 32}
Dynamic Arrays
Arrays are static in order to make them dynamic
use Re-Dim
Example:
Dim myMarks(5) as Integer
ReDim myMarks(10) as Integer
ReDim preserve myMarks(10) as Integer
Multi-Dimensional Arrays
Similar to a table
Example
Public Class TempIsZeroException : Inherits
ApplicationException
Public Sub New(ByVal message As String)
MyBase.New(message)
End Sub
End Class
User Defined Exception
If (temperature = 0) Then
Throw (New TempIsZeroException("Zero Temperature found"))
Else
Console.WriteLine("Temperature: {0}", temperature)
End If
Try
temp.showTemp()
Catch e As TempIsZeroException
Console.WriteLine("TempIsZeroException: {0}", e.Message)
End Try
Type Casting in VB .Net
Involves the conversion of a data type from one form to
another
To do so you can use the Ctype function and specify
the original variable and the resultant data type
Example
Dim array As String() = File.ReadAllLines("file.txt")
Dim line As String
For Each line In array
Dim length As Integer = line.Length
Next
Question
1. Write a program that counts the
number of a’s in a text file.
2. Write a program that stores a
username and password in a file
secret.ini, and uses the credentials
stored to authenticate a user
Next Week
Visual Basic Event driven
programming
QUESTION
Classes and Objects
A Class is a blueprint that defines what an object of that
class will consist of as well as the operations that can
be performed on the object.
Example
Class <class_name>
…..[Statements]
End Class
Instantiating a class
A class is instantiated using the New keyword in visual
basic.
Example
Class myClass
……….[Statements]
End Class
Sub Main(){
Dim newMyClass as myClass = new myClass()
}
Class Constructors and
Destructors
A class constructor is a special member Sub of a class that is
executed whenever we create new objects of that class. A
constructor has the name New and it does not have any
return type.