What is the point with Delegates in VB.Net?

J

Jarod_24

What is the point with Delegates in VB.Net

What can these things do that we can not allready do with the use of Interfaces, Events and Event handlers and so on...

I'd like a discussion on this, and some practical examples where Delegates would be better/worse to use...
 
P

Philip Rieck

Well, there's the fact that delegates are an integral part of Events and the
calling of thier Event Handlers. So the question is not what can they do
that you can't do with Events, but what can they do *other* than provide a
mechanism for Events. The answers are many and varied - they are similar
to using function pointers in C (not the same, but similar concepts).
 
I

Imran Koradia

For one - Using delegates is the only way to access winform controls from
another thread than the one they were created on (stems from the fact that
forms and controls rely on STA (Single Threaded Apartment) threading model
since they are based on the Win32 message architecture which is inherently
apartment-threaded).

Imran.
 
J

Jay B. Harlow [MVP - Outlook]

Jaron_24,
In addition to the other comments:

An Interface requires that a class implement a specific method, it can only
implement the method once. A class can have multiple methods that have the
same signature as the Delegate.

Events are implemented in terms of Delegates. Or stated another way Events
encapsulate a Delegate, similar to how Properties encapsulate other types of
Fields. This will become more evident in VB.NET 2005.

A delegate can be used for simple Callbacks, rather then requiring multiple
classes that implement an Interface. A single class can implement multiple
call back methods, then using AddressOf the specific version can be used.

Consider the following sample I posted a few days ago:

Public Class WStoryII

Public Delegate Function Predicate(ByVal item As Customer, ByVal value
As Object) As Boolean

Public Class Customer

Private ReadOnly m_dateOfSale As Date
Private ReadOnly m_item As String
Private ReadOnly m_code As String
Private ReadOnly m_name As String

Public Sub New(ByVal dateOfSale As Date, ByVal item As String, ByVal
code As String, ByVal name As String)
m_dateOfSale = dateOfSale
m_item = item
m_code = code
m_name = name
End Sub

Public Overrides Function ToString() As String
Return String.Format("Customer({0:d}, {1}, {2}, {3})",
m_dateOfSale, m_item, m_code, m_name)
End Function

#Region " Predefined predicates "

Public Shared Function CompareName(ByVal item As Customer, ByVal
value As Object) As Boolean
Return item.m_name = DirectCast(value, String)
End Function

Public Shared Function CompareDate(ByVal item As Customer, ByVal
value As Object) As Boolean
Return item.m_dateOfSale = DirectCast(value, DateTime)
End Function

Public Shared Function CompareTodayForName(ByVal item As Customer,
ByVal value As Object) As Boolean
Return item.m_dateOfSale.Date = DateTime.Today AndAlso
item.m_name = DirectCast(value, String)
End Function

#End Region

End Class

Public Class CustomerCollection
Inherits CollectionBase

Public Sub Add(ByVal dateOfSale As Date, ByVal item As String, ByVal
code As String, ByVal name As String)
Me.InnerList.Add(New Customer(dateOfSale, item, code, name))
End Sub

Default Public ReadOnly Property Item(ByVal index As Integer) As
Customer
Get
Return DirectCast(Me.InnerList.Item(index), Customer)
End Get
End Property

Public Function Find(ByVal value As Object, ByVal predicate As
Predicate) As Customer
For Each item As Customer In Me.InnerList
If predicate.Invoke(item, value) Then
Return item
End If
Next
Return Nothing
End Function

End Class

Public Shared Sub Main()
Dim customers As New CustomerCollection
customers.Add(DateTime.Today, "A1", "A", "Jay")
customers.Add(DateTime.Today.AddDays(-1), "B1", "B", "WStoryII")
customers.Add(DateTime.Today.AddDays(1), "C1", "C", "Sam")

Dim item As Customer

item = customers.Find("WStoryII", AddressOf Customer.CompareName)
Debug.WriteLine(item, "find name")

item = customers.Find(DateTime.Now, AddressOf Customer.CompareDate)
Debug.WriteLine(item, "find now")

item = customers.Find(DateTime.Today, AddressOf
Customer.CompareDate)
Debug.WriteLine(item, "find today")

End Sub

End Class

The CustomerCollection.Find routine uses a Delegate to call one of many
different methods, such as CompareName, CompareDate & CompareTodayForName.
With an Interface, would have required creating a CompareName class, a
CompareDate class, and a CompareTodayForName class... Or I could have
defined an Enum for the field to compare, however then complex comparisons
such as CompareTodayForName would not have been possible.

Hope this helps
Jay
 
T

Tom Dacon

Delegate: TYPE-SAFE function pointer.

'nuff said.

Tom Dacon
Dacon Software Consulting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top