Attention: We are retiring the ASP.NET Community Blogs. Learn more >

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?

5 Comments

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

  • Generics in VB:

    http://weblogs.asp.net/pwilson/archive/2003/10/27/33936.aspx

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

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

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



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



Comments have been disabled for this content.