473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

What is the point with Delegates in VB.Net?

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...
Nov 21 '05 #1
4 3402
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).

"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...

Nov 21 '05 #2
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.

"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...

Nov 21 '05 #3
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...

Nov 21 '05 #4
Delegate: TYPE-SAFE function pointer.

'nuff said.

Tom Dacon
Dacon Software Consulting

"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...

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: e-mid | last post by:
i used mouseEvents or clickEvents etc.. but i did not still understand what an event is yet. eg: when we press a button, the eventhandler registered with the the button's click *event* is...
2
by: al | last post by:
Greetings, Can someone please tell me the what is the defference betweeen delegates and events handler??? MTIA, Grawsha
8
by: Jeff S. | last post by:
I was recently advised: << Use List<struct> and Find() using different Predicate delegates for your different search needs.>> What is "Predicate delegate" as used in the above recommendation? ...
6
by: Jon Davis | last post by:
I've used delegates fairly heavily for several years in C# for event handling and for starting threads. Does anyone have any real-world scenarios where delegates were both extremely useful and...
5
by: jm | last post by:
If I have multiple subscribers to a delegate, what keeps track of them? Are they out there on the heap? When I declare a delegate: public delegate void MyDelegate (object myObject,...
22
by: Jon Skeet [C# MVP] | last post by:
I'm looking to write a C# book fairly soon, and the publisher I've approached wants me to do a bit of market research to find out what people like and don't like in this kind of book. I've read...
6
by: =?Utf-8?B?T2xkQ2FEb2c=?= | last post by:
My question is regarding the use of delegates in C#. I see how .Net uses delegates to wire event handlers to events. It’s an object created by a single line of code by the system and that makes...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.