Wrox Programmer Forums
|
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 26th, 2004, 11:05 AM
Authorized User
 
Join Date: Jul 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to p_nut33 Send a message via MSN to p_nut33
Default I still Don't get Delegates

Why would I use a delegate If I can just fire up the event by itself?

I really don't get delegates at all.

1) I declare an event in an object
2) I declare an object with the with events
3) I create a method/procedure/function with the handles object.event

Now where does a delegate come in handy here? Im really struggling with this. Don't tell me a delegate is a pointer to a function....heh heh.

Thanks
 
Old April 26th, 2004, 12:53 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

A delegate is a pointer to a function.

As such, events are implemented internally using delegates, but that isn't the only thing they are used for, although that by far is the most visible and common use. Callback procedures (both synchronous and asynchronous) are another use. Other, perhaps more esoteric uses allow for a certain kind of polymorphism - different procedures can be called depending on run-time conditions using a delegate. Admittedly, this could be done any number of ways (e.g. IF/ELSE or SELECT CASE constructs), but a delegate is generally more efficient and certainly more .

Note that the handles clause on a procedure in fact sets up a delegate behind the scenes for you - that's why you can use any name you choose for the event procedure - it's actually the delegate that establishes the link between your procedure and the event, unlike VB6 which required you to name an event procedure a certain way.

Note also that one procedure can handle multiple events from multiple sources, again all handled via the behind-the-scenes delegate.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old April 26th, 2004, 01:50 PM
Authorized User
 
Join Date: Jul 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to p_nut33 Send a message via MSN to p_nut33
Default

I think my confusion has been spawned by examples that use events and delegates at the same time. But I think I get what you are saying. Basically I could be done with events and use delegates exclusively to achieve the same functionality. Call a particular sub/function which is basically the same as a sub/function tied to an handles. So then, why do people use combinations of both. For example, right now I am studying this remoting example where the author declares a delegate with a signature and then creates two events as the delegate and basically uses the "handles obj.event" instead of using the addressOF functionality of the delegate. This is what's been confusing me.

Anyways enough rant! Thanks!

Quote:
quote:Originally posted by Jeff Mason
 A delegate is a pointer to a function.

As such, events are implemented internally using delegates, but that isn't the only thing they are used for, although that by far is the most visible and common use. Callback procedures (both synchronous and asynchronous) are another use. Other, perhaps more esoteric uses allow for a certain kind of polymorphism - different procedures can be called depending on run-time conditions using a delegate. Admittedly, this could be done any number of ways (e.g. IF/ELSE or SELECT CASE constructs), but a delegate is generally more efficient and certainly more .

Note that the handles clause on a procedure in fact sets up a delegate behind the scenes for you - that's why you can use any name you choose for the event procedure - it's actually the delegate that establishes the link between your procedure and the event, unlike VB6 which required you to name an event procedure a certain way.

Note also that one procedure can handle multiple events from multiple sources, again all handled via the behind-the-scenes delegate.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with delegates mike_remember ASP.NET 1.0 and 1.1 Professional 2 November 7th, 2006 12:33 AM
Delegates RalphJr C# 2005 1 April 29th, 2006 08:06 AM
Delegates pramos.21d C# 1 April 11th, 2006 03:43 AM
Help with Delegates mike_remember ASP.NET 1.0 and 1.1 Basics 4 October 4th, 2005 07:32 AM
Delegates Ibn_Aziz C# 0 February 3rd, 2004 05:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.