You are on page 1of 18

Unit III

Object oriented Programming in


VB.NET
1 Sub Procedures and Functions
• A procedure contains a series of statements
that are executed when called.
• After execution of a procedure the control
returns to the statement calling the
procedure.
• Function –returns values
• Sub procedure –does not return values
1.1 Sub Procedures
• The Sub statement is used to define procedures
• Sub keyword indicates start of a Sub procedure
• End keyword indicates end of procedure
• Modifiers specify the access level of the procedure,
possible values are
• Private
• Protected
• Public
• Friend
Syntax
[modifiers]Sub Subname[(parameter list)]
[Statements]
End Sub

• Sub name – name of procedure


• Parameters- list of parameters
WAP to find area of circle
Module Module1
Sub area(ByVal r As Double)
Dim a As Double
a = 3.14 * r * r
Console.WriteLine("area of circle " & a)
MsgBox("area: " & a)
End Sub

Sub Main()
Call area(4)

Console.ReadKey()
End Sub

End Module
We can pass parameters to procedure by 2
methods
1 parameters by value(ByVal):
ByVal means that a copy of the provided value
will be sent to the procedure
2 parameters by reference(ByRef):
ByRef in means that a reference to the original
value will be sent to the procedure.
1.2 Function
• The function statement is used to define
functions like a Sub procedure but it can
return a value of a particular data type.
Syntax
[modifiers]Function functionname[(parameter
list)] As returntype
[statement]
End Function
WAP to find area of circle
Function area(ByVal r As Double) As double
Dim a As Double
a = 3.14 * r*r
Return a
End Function

Sub Main()
Dim a1 As Double
a1 = area(4)
Console.WriteLine("area" & a1)
Console.ReadKey()

End Sub
2 Class and object
• Class is a collection of data members, methods
and properties and events
• It is used to define new datatypes
• Class is referred as user defined datatype.
• Class is a blue print for an object
• To use class we need a object
Syntax
• [Access specifiers] Class Classname
[variables]
[methods]
[properties]
[events]
End Class
• Variables or fields to represent data
• Methods or operations to performed on that
data
• Properties which are used to access the
private data
• Events to perform some activities when occur.
2.1 Field in class
• Field is a member of a class which holds data
• Syntax:
[Access specifiers] variable_name As [Datatype]

• Access constraints are defined using access


specifiers
• datatype of a variable states which type of
data it can hold
Example of field
Public Class Employee
private empname As String
private empid As Integer
End Class
2.2 Object
• An object is a real time entity which can be used to access
the class
• It is a instance of a class
• Object is used to access the members of a class
• To declare an object “Dim” statement and “New” keyword
is used.
• Eg: Dim obj as New classname()

• To access methods
• Objectname.Methodname()
Syntax
Dim objectname As classname= New classname
Or

Dim objectname As New Classname()


Simple Program
Public Class test
Sub disp()
Console.WriteLine("Welcome ")
End Sub

End Class
Sub Main()
Dim obj As New test
obj.disp()

Console.ReadKey()

End Sub
3 Access specifiers
• Private – private members are accessed within class
• Public – public members are accessed to anyone
• Friendly- they are accessible to methods of any class in
friend class(within class +within project )
• protected- can be accessible from containing class
types inherited from containing class(within
class+derived class).
• Protected friendly- they are accessible to methods of a
class, methods of derived class from it(derived class+
+within class +within project).

You might also like