View
21
Download
0
Category
Tags:
Preview:
DESCRIPTION
Object Oriented Programming. Procedure for Defining a Class Declaring Methods What Is Inheritance? Using Constructors Using Destructors Declaring Properties Examples. Procedure for Defining a Class. To define a class in Visual Basic .NET, you can follow this general procedure: - PowerPoint PPT Presentation
Citation preview
OBJECT ORIENTED PROGRAMMING
PROCEDURE FOR DEFINING A CLASS
DECLARING METHODS
WHAT IS INHERITANCE?
USING CONSTRUCTORS
USING DESTRUCTORS
DECLARING PROPERTIES
EXAMPLES
• To define a class in Visual Basic .NET, you can follow this general procedure:
1. Add a class to the project.2. Provide an appropriate file name for the class when you add it. This will name both the file and the class itself. If you do not change the file name when you add it, you can change the class name at any time by changing theclass definition in the code window.3. Create constructors as needed.4. Create a destructor if appropriate.5. Declare properties.6. Declare methods and events.
Note: In Visual Basic .NET, you can define more than one class in a single file. You are not limited to one class per file,
PROCEDURE FOR DEFINING A CLASS
• As shown in the following example:
Public Class TestClassPublic Sub TestIt(ByVal x As Integer)...End Sub
Public Function GetIt( ) As Integer...End Function
End Class
DECLARING METHODS
WHAT IS INHERITANCE?• can use inheritance to derive a class from an existing base class. The
derived class can inherit all the base class properties, methods, data members, events, and event handlers, making it easy to reuse the base class code throughout an application.
• The following example shows how to use the Inherits keyword to define a derived class that will inherit from an existing base class:
Public Class DerivedClassInherits BaseClass...End Class• use the MustInherit keyword to define classes that are not intended to
be used directly as instantiated objects. The resulting class must be inherited as a base class for use in an instantiated derived class object. If the client code attempts to instantiate an object based on this type of class, a compiler error is generated, as shown in the following example:
Public MustInherit Class BaseClass...End Class...'Client codeDim x As New BaseClass( ) 'Generates a compiler error
WHAT IS INHERITANCE?
USING CONSTRUCTORS
• you control the initialization of new objects by using procedures called constructors(The Sub New constructor) and has the following features:
• The code in the Sub New block will always run before any other code in a class.
• the Sub New constructor will only run once: when an object is created.• Sub New can only be called explicitly in the first line of code of another
constructor, from either the same class or a derived class using the MyBase keyword.
• Every class has a constructor. Even when you don't define one, a default constructor is added in the background
• The default constructor looks like this :
Public Sub New() MyBase.New() End Sub
• Example:
USING DESTRUCTORS
• Can control what happens during the destruction of objects by using procedures called destructors.
• Destructors or finalizer is a method used to deallocate the resources of an object that are no longer used, its invoked automatically by the VB.net environment.
• The Finalize method is a Sub procedure that should always be protected and overrides the Finalize method of the base class. However, unlike a constructor, the Finalize method of the base classes in the hierarchy do not run unless explicitly called with MyBase.Finalize in the derived class .
• Example: Protected Overrides Sub Finalize()
Console.WriteLine("Calling Destructor")
Ens Sub
• who has the control over the destructor (in VB.NET)? it's the .Net frameworks Garbage Collector (GC).
• Now few questions arise why GC should control destructors why not us? • GC can do better object release than we can.• There is chance that one can forgot de-allocation• time consuming and complex process.
• If program ends GC automatically reclaims all the memory occupied by the objects in the program.
• GC keeps tracks of all the objects and ensures that each object gets destroyed once.
• GC ensures that objects, which are being referenced, are not destroyed.• GC destroys the objects only when necessary. Some situations of
necessity are memory is exhausted or user explicitly calls System.GC.Collect() method.
USING DESTRUCTORS
• When client code has no further need for an object’s resources, it can directly call code placed in the Dispose method of the object. However, note that you may cause an exception if you attempt to run the Dispose code twice. An exception can occur if you have already closed or released an object.
'Class codePublic Sub Dispose( )'Check that the connection is still open...conn.Close 'Close a database connectionGC.SuppressFinalize(Me)End SubProtected Overrides Sub Finalize( )Dispose( ) 'Optional call to DisposeEnd Sub
USING DESTRUCTORS
'Client codeDim x as TestClass = New TestClass( )...x.Dispose( ) 'Call the object's dispose method
• if the client code called the Dispose method directly, the connection would be closed and the Finalize method would not be called by garbage collection. If the client did not call the Dispose method, the Finalize method will still execute when garbage collection destroys the object.
USING DESTRUCTORS
DECLARING PROPERTIES
• declare your properties by using two code blocks in a single procedure, as follows:
Private intMyData As Integer
Public Property MyData( ) As IntegerGetReturn intMyDataEnd Get
Set (ByVal Value As Integer)intMyData = ValueEnd Set
End Property
USING READ-ONLY PROPERTIES
• You can create read-only properties by using the ReadOnly keyword when you declare the property.
Public ReadOnly Property MyData( ) As Integer
GetReturn intMyDataEnd Get
End Property
Note: You cannot use the Set block when defining read-only properties because the property cannot be updated. The compiler will generate an error if you attempt to do this.
USING WRITE-ONLY PROPERTIES
• You can create write-only properties by using the WriteOnly keyword when you declare the property.
Public WriteOnly Property MyData( ) As Integer
Set (ByVal Value As Integer)intMyData = ValueEnd Set
End Property
Note: You cannot use the Get block when defining write-only properties because the property is not readable. The compiler will generate an error if you attempt to do this.
Class Example Private _value As Integer Public Sub New() _value = 2 End Sub Public Function Value() As Integer Return _value * 2 End Function End Class Module Module1 Sub Main() Dim x As Example = New Example() …………. constructor Console.WriteLine(x.Value()) End Sub End Module
WHAT IS THE OUTPUT
Class A Public _value As Integer Public Sub Display() Console.WriteLine(_value) End Sub End Class Class B : Inherits A Public Sub New(ByVal value As Integer) MyBase._value = value End Sub End Class Class C : Inherits A Public Sub New(ByVal value As Integer) MyBase._value = value * 2 End Sub End Class Module Module1 Sub Main() Dim b As B = New B(5) b.Display() Dim c As C = New C(5) c.Display() End Sub End Module
WHAT IS THE OUTPUT
Module Exercise1Task Sub Main() Dim object2 As Class2 = New Class2 ' Console.WriteLine(object2.Value1) Console.WriteLine(object2.Value2) ' Console.ReadLine() End Sub End Module Class Class1 Public Value1 As Integer Public Sub New() Value1 = 1 End Sub End Class Class Class2 : Inherits Class1 Public Value2 As Integer End Class
WHAT IS THE OUTPUT
Module Exercise2Task Sub Main() Dim object4 As Class4 = New Class4 ' Console.WriteLine(object4.Value1) Console.WriteLine(object4.Value2) ' Console.ReadLine() End Sub End Module Class Class3 Public Value1 As Integer Public Sub New() Value1 = 3 End Sub End Class Class Class4 : Inherits Class3 Public Value2 As Integer Public Sub New() Value2 = Value1 * 4 End Sub End Class
WHAT IS THE OUTPUT
Class A Public Sub New() Console.WriteLine("In class A constructor") End Sub Protected Overrides Sub Finalize() Console.WriteLine("In class A destructor") MyBase.Finalize() End Sub End Class Class B Inherits A Public Sub New() Console.WriteLine("In class B constructor") End Sub Protected Overrides Sub Finalize() Console.WriteLine("In class B destructor") MyBase.Finalize() End Sub End Class Class C Inherits B Public Sub New() Console.WriteLine("In class C constructor") End Sub Protected Overrides Sub Finalize() Console.WriteLine("In class C destructor") MyBase.Finalize() End Sub End Class Module Module1 Sub Main() Dim objSample As New C() End Sub End Module
WHAT IS THE OUTPUT
File: Circle.vb
Public Class Circle Private rad As Double Public Sub New() rad = 0 End Sub Public Sub New(ByVal r As Double) rad = r End Sub Public ReadOnly Property Radius() Get If rad < 0 Then Return 0 ' else is implied Return rad End Get End Property End Class
File: Exercise.vb
Module Exercise Public Function Main() As Integer Dim circ As Circle = New Circle(-64.25) Console.WriteLine(" -=- Circle Characteristics -=-") Console.WriteLine("Radius: {0}", circ.Radius) Console.WriteLine() circ = New Circle(38.18) Console.WriteLine(" -=- Circle Characteristics -=-") Console.WriteLine("Radius: {0}", circ.Radius) Console.WriteLine() Return 0 End Function End Module
WHAT IS THE OUTPUT
Recommended