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
"Jarod_24" <ja******@hotmail.com> wrote in message
news:1097027892.JN22f40PLEge4zk4LIm1Mw@teranews...
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...