Jeff and .NET

The .NET musings of Jeff Putz

Sponsors

News

My Sites

Archives

Need help on defining delegates

I'm at that point in my book where I need to write about delegates. While I do “get” delegates, it's really hard for me to put into words what they are. Every example I've seen in other books does a poor job of it, in part because the descriptions are so abstract that you end up scratching your head and wondering why you'd ever ever need one.

So before I spoil your thoughts with the direction I think I want to go, you tell me what you'd say. My audience is ASP.NET developers making the leap to OOP, probably from more of a script-heavy background.

What do you think?

By the way, what's the syntax for generics (Whidbey) in VB.NET?

Posted: Mar 22 2004, 02:00 PM by Jeff | with 7 comment(s)
Filed under:

Comments

Memi Lavi said:

I would define a Delegate as a pointer to a function. It defines the signature of the function, but it's not the function itself.

One scenario in which I used delegate was this:
I had a base page which had a method that iterated hierarchicaly through all the controls on the page (would you believe that there is nothing like that built in the ASP.NET page model?) Problem was, I needed to tell the method what to do with all the controls it finds. For example, sometimes I wanted to put all the control values in a Name-Value collection. Sometimes I wanted to disable all these controls. And there sure will be other uses for this method.
What I did was to define a delegate that accept as an argument a WebControl, and add a parameter of this delegate type to the iterating method. Each time I needed this method, I created a new method with the same signature as the delegate, and passed its address to the iterating method. The iterating method then called the delegate with the appropriate parameter whenever it encountered a control on the page, and the new method did whatever was needed with this control.

Hope it helps.
# March 22, 2004 2:42 PM

Jason Mauss said:

Jeff - I wrote this article for my site a long time ago: http://www.vsdotnetguru.com/Articles.asp?aid=9

The analogy seemed to be well received. Feel free to steal it if you want.
# March 22, 2004 3:02 PM

Paul Wilson said:

# March 22, 2004 3:19 PM

Julie Lerman said:

When I was first moving from VB6 to .net, delegates was something that I just could not get a handle on (no pun intended). I looked everywhere for an explanation that worked for me and it finally clicked when reading Matthew MacDonald's "The Book of VB.NET" (no starch press). I have sinced passed the book on to someone in my user group so I can't give you the exact reference. But whatever it was that he wrote was just the right analogy for me.
# March 22, 2004 5:56 PM

AndrewSeven said:

I kind of like the dictionary.com entry ;)
http://dictionary.reference.com/search?q=delegate

Events are a little more concrete, they might be a good way to sneak up on the delegates.
# March 22, 2004 7:28 PM

Johnny Hall said:

Try this. I always point my new .NET guys here first...

http://www.sellsbrothers.com/writing/default.aspx?content=delegates.htm

# March 23, 2004 11:19 AM

TrackBack said:

# April 7, 2004 1:50 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)