Upload
ashwini-yadav
View
90
Download
3
Embed Size (px)
DESCRIPTION
Citation preview
2. Example: Public Interface vehicle Sub driveBike() Sub driveCar() End Interface Public Class Pune : Implements vehicle Public Sub driveBike() Implements vehicle.driveBike Console.Writeline(I m driving Bike in Pune) End Sub Public Sub driveCar() Implements vehicle.driveCar Console.Writeline(I m driving Car in Pune) End Sub End Class Public Class Mumbai: Implements vehicle Public Sub driveBike() Implements vehicle.driveBike Console.Writeline(I m driving Bike in Mumbai) End Sub Public Sub driveCar() Implements vehicle.driveCar Console.Writeline(I m driving Car in Mumbai) End Sub End Class Sub Main() Dim M As New Mumbai() Dim P As New Pune() P.driveBike() P.driveCar() P.driveBike() P.driveCar() End Sub 3. Delegates A function pointer is a special type of variable that is used to access a function using its address. To support function pointers, the .NET Framework provides the concept of delegates. A delegate is not a real function. It provides a syntax for a function that it would be associated with. Delegates are pointers that are used to store and transfer information like the memory address, event handled by functions and subroutines. Delegates are type safe, since they check for the signatures of functions and subroutines only if same, they transfer information. A delegate is declared using the keyword Delegate to a function or procedure name. 4. Creating a Delegate: [modifier] Delegate Sub/Function Name (parameter(s)) As ReturnType The modifier factor can be Public, Private, or Friend. This is followed by either the Delegate Sub or the Delegate Function expression. A delegate must have a name: The Name factor of this formula. The name follows the rules we have been observing for valid names of the Visual Basic language. Because a delegate is some type of a template for a procedure, you must use parentheses. If this procedure will not take any argument, you can leave the parentheses empty. If the delegate will be associated with a sub-procedure, there is no return type. Here is an example: Module Exercise Delegate Sub Messenger() End Module 5. Module Module1 Public Delegates Sub Firstdelegate (ByVal x As integer, ByVal y As Integer) Public Sub Add(ByVal x As integer, ByVal y As Integer) Console.Writeline(Addition is, x+y) End Sub Public Sub Mul(ByVal x As integer, ByVal y As Integer) Console.Writeline(Multiplication is, x*y) End Sub Sub Main() Dim Del, Del1 As Firstdelegate object of delegate Del=AddressOf Add Assign Address of function Del1=AddressOf Mul Del.Invoke(10,20) used to invoke or call the function Del1.Invoke(5,3) End Sub 6. Delegates are of two types: Single-cast delegates: * If a delegate invokes a single method, then it is called as a Single cast delegate. * Single-cast Delegates refer to a single method with matching signature. They are derived from the System.Delegate class Multi-cast delegates: *They are nothing but a single delegate that can invoke multiple methods of matching signature. *Derives from System.MulticastDelegate class which is a subclass of System.Delegate *In this, we create a single delegate that in turn invokes multiple encapsulated methods. We can use Multi-cast Delegates when multiple calls to different methods are required.