ARTICLE

Delegates in VB.NET

Posted by TimothyA Vanover Articles | Visual Basic 2010 February 04, 2001
The Zip file contains two projects. One is VB.Net and the other is C#. Each project is an example of the same use of a delegate. A base class is derived and the delegate calls a method on several classes derived from the base class. Several things are shown from this example. Using inherited base types to strinct type checking of a base type and calling class level methods from a single delegate.
Download Files:
 
Reader Level:

The Zip file contains two projects. One is VB.Net and the other is C#. Each project is an example of the same use of a delegate. A base class is derived and the delegate calls a method on several classes derived from the base class. Several things are shown from this example. Using inherited base types to strinct type checking of a base type and calling class level methods from a single delegate.

Here is sample code from Delegates.vb file:
 

Module Module1
Public Class Wisdom 'Class containing Delegate
Delegate Function GiveAdvice() As String
Public Function OfferAdvice(ByVal Words As GiveAdvice) As String
Return (Words())
End Function
End Class
Public Class Parent 'Base type
Public Overridable Function Message() As String
Return ("You should listen to your elders")
End Function
End Class
Public Class Dad 'derived from the base parent type
Inherits Parent
Public Overrides Function Message() As String
Return ("You should listen to your Mom")
End Function
End Class
Public Class Mom 'derived from the base parent type
Inherits Parent
Public Overrides Function Message() As String
Return ("You should listen to your Dad")
End Function
End Class
Public Class Daughter 'not derived from the base parent type
Public Function Message() As String
Return ("I know all there is to life")
End Function
End Class
Public Function CallAdvice(ByVal p As Parent) As String
Dim objParents As New Wisdom
Dim TeenAgeGirl As Wisdom.GiveAdvice = New Wisdom.GiveAdvice(AddressOf
p
.Message)
Return (objparents.OfferAdvice(TeenAgeGirl))
End Function
Sub Main()
Dim objDad As New Dad
Dim objMom As New Mom
Dim objDaughter As New Daughter
console.WriteLine(CallAdvice(objDad))
console.WriteLine(CallAdvice(objMom))
'will not work as this is not derived
'console.WriteLine(CallAdvice(objDaughter))
End Sub
End
Module

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor