0% found this document useful (0 votes)
30 views

Oop

OOPs in VB.Net
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Oop

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

OOP

&
VB.NET
OOP
• Object Oriented Programming
• Deals with objects that have attributes and behaviors.
• Classes
• Is an prototype to create object
• Objects
• Is an entity that has a state and exhibits behavior
• Data Encapsulation
• Process of storing all the data members and method in a single unit
called class.
• Data Abstraction
• Process of hiding the complex details of a system and providing
only the essential features.
• Inheritance
• Inherits the members that have been already created along with
the existing members.
• Polymorphism
• ability to exist is more than one form.
Benefits
• Reusability
• Using the features of inheritance members and methods in a class can be used effectively
by the objects of an inherited class.

• Flexibility to change
• If the method has been changed that will be applicable to all the classes that inherits that
class.

• Data security
• Depending upon the security and accessibility data members can be declared private or
public
Creating Classes
• Class
• Entities that share common attributes and behaviors.
Syntax to create class
Class <<classname>>
End Class

• Class members that can be either


data objects or member functions.
Syntax of a class menmber
Class <<classname>>
Public Sub <<methodname>>
End Sub
End Class
Creating Classes
• Object
• Declare a variable of the type class referred as object.
• Accessed using a simple dot notation.
Syntax
Dim <<objectname>> as New <<classname>>
<<objectname>>.<<methodname>>
Example
Public Class Area
Public Sub Square(side as Integer)
End Sub
End Class
Dim are1 as Area
are1.Area(10)
Class Library Component
• To create a class or a reusable
component that exposes functionality
that can be used in various projects.
• Two types of applications exists
• Server class application
• Defines the class with its methods and
attributes
• Consumer client application
• Implements the methods of the class using
objects
Class Library Component
Class Library Component
Class Library Component
Class Library Component
Class Library Component
Class Library Component
Class Library Component
Class Library Component
Class Library Component
Class Library Component
Class Library Component
Class Library Component
Class Library Component
Class Library Component
Class Library Component
Class Library Component
Class Library Component
Class Library Component
Access Modifiers
• Specifies the availability of class and its members to
other function or program.
• Restricted to provide access to a class and to the
members of the class
• Applicable to the class are also applicable to the
members.
Access Modifiers
• Public
• The member can be accessed by any object, class or method.
Example
Public sub Square()
End Sub
• Protected
• The member can be accessed only within the class and from a derived class.
Example
Protected sub Square()
End Sub
• Friend
• The member can be accessed only within same assembly that contains it.
• Any compiled code becomes assembly.
Example
Friend sub Square()
End Sub
• Protected Friend
• The member can be accessed from the same assembly that contains the member and also
from the derived class
Example
Protected Friend sub Square()
End Sub
• Private
• The member can be accessed only within the context where it has been declared.
Example
Private sub Square()
End Sub
Methods, properties & Events
• Methods
• Group of statements.
• Properties
• Attributes of an application that is used to access the
private variables.
• Events
• Notifications sent to the application to perform predefined
actions.
Methods
• Methods
Declaration
<<access modifier>> Sub <<method name>>([<<parameter list>>])
End Sub
Parameters are optional in Methods.
Any number of parameters it can have.
Example
Public Sub display()
Console.WriteLine(“Hello”)
End Sub

Dim out as Integer


Public Sub Square(ByVal r as Integer)
out=r*4
End Sub
Methods
• Funtions
• Functions returns value.
Declaration
<<access modifier>> Function <<function name>>
(<<Parameter list>>) As <<datatype>>
Return <<variablename>>
End Function
Example
Public Function Square(ByVal r as Integer) asInteger Return 4*r
End Sub

Dim out as Integer


out=Square(4)
Console.WriteLine(out)
Properties
• Value types that allow access to
underlying private variables
• Equivalent to public variables
• A class with a public variable is similar
to a class with a public property.
• Property is more secure
• The data is stored in the underlying
private variables.
Properties
• Set method
• To set the value for the variable.
• Get method
• To retrieve the value of the variable.
• Three types
• ReadWrite
• The get and set method can be used.
• ReadOnly
• Only get method can be used.
• WriteOnly
• Only set method can be used.
Properties
• Syntax

Public Property <<PropertyName>> as <<Datatype>>


Get
Return <<variablename>>
End Get
Set(ByVal Value as <<Datatype>>)
<<variablename>> = value
End Set
End Property
Properties
• ReadWrite
Example to declare a property
Public Class Employee
Private EmployeeName as String
Public Property EmpName as String
Get
Return EmployeeName
End Get
Set(ByVal Value as <<Datatype>>)
EmployeeName = value
End Set
End Property
End Class

Example to use the property


Dim ob j as new Employee
obj.EmpName=“Kala”
Console.WriteLine(obj.EmpName)
Properties
• ReadOnly
Example to declare a property
Public Class Employee
Private EmployeeName as String = “Kala”
Public ReadOnly Property EmpName as String
Get
Return EmployeeName
End Get
End Property
End Class

Example to use the property


Dim ob j as new Employee
obj.EmpName=“Kala” ‘throw an error
Console.WriteLine(obj.EmpName)
Properties
• ReadOnly
Example to declare a property
Public Class Employee
Private EmployeeName as String
Public WriteOnly Property EmpName as String
Set(ByVal Value as <<Datatype>>)
EmployeeName = value
End Set
End Property
End Class

Example to use the property


Dim ob j as new Employee
obj.EmpName=“Kala”
Console.WriteLine(obj.EmpName) ‘throw an error
Events
• Declaration
Event <<EventName>>(ByVal <<variable>> as <<datatype>>)
Example
Event UserEvent(ByVal EventNo as Integer)
• Declares a user-defined event that can be called wheneever a certain condition is met.
• Can declare inside a class or a structure.
• Acts as a message informing an application to attend to a user or a system request.
• To raise the event use
Syntax
RaiseEvent <<EventName>>
Encapsulation
• Exposing the functionality of a class through properties and
methods while hiding the actual implementation.
Example
Class Automobile
Private Color as String
Private Tires As Integer
Public Function Drive()
‘Implementation of the drive method
End Function
Public Property BodyColor() as String
Get
Return Color
End Get
Set(ByVal Value as String)
Color=Value
End Set
End Property
Public Property NumberOfTires() as Integer
Get
Return Tires
End Get
Set(ByVal Value as String)
Tires=Value
End Set
End Property
End Class
Inheritance
• Enables one class to inherit the resources of another class.
• Achieve significant code reuse.
• Objects of one class acquire the properties of objects of another class.
• Two types of class
• Super Class or Base Class
• The class that provides the resource to another class.
• Sub Class
• The Class that inherits the resource from another class.
Inheritance
• Inherits is the keyword used to inherit the class
Syntax
<<access modifiers> Class <<Subclass>>
Inherits <<SuperClass>
End Class
Example
Public Class Motor
Inherits Automobile
End Class
Public Class Car
Inherits Automobile
End Class
Inheritance

• Visual Inheritance
• Create a normal window application
• Generate the code
• To inherit ProjectAdd Inherit form
 Open
Inheritance
Inheritance
Polymorphisim

• Ability of an object to exist in multiple


forms
• Redefining the methods on the
subclass that have been inherited
from the base class
• Overriding Methods
Namespaces
• Created class are part of Namespaces.
• Provides hierarchical grouping of types such as
classes and interfaces.
• Container containing related types
Example
System.Console
System.Data
Namespaces
• Creating Namespaces
Syntax
Namespace <<namespacename>>
End Namespace
• Using Namespaces
• Import Keyword is used to import namespaces.
Syntax
Imports <<namespacename>>
• Classes in the .NET framework class library can be accessed using System
namespaces
Namespaces
• Rules
• Don’t specify access type for a namespace
• Public in default
• Can’t declare a Private class inside a namespace
Example
Namespace Bank
Class SBAcc
End Class
End Namespace
Valued Data Types
• User Defined value data types

• Structures
• Stores multiple values in the form of a record that can be of same type or of
different types.
• Enumeration
• Named constants
• Values with unique names so that the values can be identified globally.
Structures
• Creating Structures
• Declare a Structure.
• Store the information inside it.
Syntax
Structure <<structurename>>
‘Declaration of variables
End Structure
Example
Structure Employee
Public emp_fname as String
Public emp_lname as String
Public emp_age as Double
Public emp_dob as Date
End Structure
Structures
• Accessing data
• Create objects of the structure
• Using the objects the members can be accessed by the dot
notation.

Example
Dim empobj as Employee
empobj.emp_fname=“Kala”
empobj.emp_lname=“Mala”
empobj.emp_age=10
Structure Vs Classes

Class Structure
Inheritable Not inheritable
Have constructors with or without Have constructors only with
parameters parameters
Have reference-type variables Have value type variables
Members can be initialized within Members cannot be initialized
the class declaration within the Structure declaration.
Enumeration
• Creating Enumeration
• Has an underlying base datatype which must be
numeric
• Every enumeration contains
• Name
• Underlying data type (must be Integer)
• Set of data
Syntax
<<accessmodifier>> Enum <<enumname>>
As <<numeridatatype>> <<varnam>> = [value]
End Enum
Example
Public Enum shapes
Square
rectangle
Circle
End Enum
Enumeration
• Assigning Values
• Assigning values to the variable is possible
Example
Public Enum shapes
Square =5
rectangle=10
Circle=15
End Enum

Public Enum shapes


Square
rectangle=10
Circle
End Enum
Enumeration
• Using Enumeration
• Create an object
• Access using dot notification
Example
Public Enum shapes
Square =5
rectangle=10
Circle=15
End Enum
Dim num as Integer
Public Function GetShapes() As shapes
Dim sha as shapes
Select Case num
case Is =5 : Return(sha.Square)
case Is =10 : Return(sha.rectangle)
case Is =15 : Return(sha.circle)

End Select
End Function
Referenced Data types
• Stores a pointer to another memory
location that contains data.
• Two kinds exist
• Arrays
• Classes
• Arrays
• Store multiple values of same data type using a single
name
• Values are accessed using indexes.
• Index starts from zero
• Types of Array
• Single-dimensional array
• Multi-dimensional array
• Array of array or jagged array
• Dynamic array
Single dimensional Array
• Provide name, size and data type
• Declared using Dim command.
Syntax
Dim <<arrayname>>(<<maxsize>>) as <<datatype>>
Example
Dim arr(15) as Integer
• Indexes starts from 0
• arr can store 16 values of Integer data types
Single dimensional Array
• Initializing the array
• Locate the array element using its indexes.
Example
arr(0)=1
arr(1)=10
arr(2)=20
Dim score(3 to 5 ) as Integer
• Indexes number has been explicitly
declared
• Accessing Array elements
• Done by indexes
Example
if arr(0)>0 then
Console.WriteLine(“Present”)
End if
Multi Dimensional Array
• An array having a rank more than one is called as
multi-dimensional array
• Same as table having n number of columns.
• Each column represents the entity.
• Each row represents the attribute
• Two-dimensional is the simplest form
• Has rows and columns
Multi Dimensional Array
• Creating a multi-dimensional array
• Specify the number of rows and columns
Example
Dim arr(4,5) as Integer

• Initialization
• Done by indexes
• Needs both indexes to locate
Example
arr(1,3) =20

• Accessing Elements
• Same as initialization
Example
if arr(1,3)>0 then
Console.WriteLine(“Present”)
End if
Jagged Array

• Containing another array as one of its


fields is called as jagged array
• Multi-dimensional array with irregular
dimensions
• Has arrays as its elements
• Each row of a jagged array can be
processed as an independent array
Jagged Array

• Declaration
• Multiple sets of paranthesis
Example
to declare arr(),arr1(a,b),arr2(c,d,e)
Dim arr()() As String=(New String() {“arr1”},
New String(){“a”,”b”}, New String(){“c”,”d”,”e”}}

• Initialization
• Specify the location using the indexes
Example
arr(2,2)=34
Dynamic Arrays
• At the time of creation the number of
elements are not known the size of the
array can be specified later
• This is called as Dynamic array
• Declare the array without specifying the
size
Example
Dim arr() As Integer

• As the size of the array is known declare


the array size using ReDim command
Example
Dim arr(x) As Integer

You might also like