Friday, July 08, 2005

Sandy Roach began the group meeting with a presentation of delegates and event handling.  Sandy tried to show us some simple examples of delegates in Visual Basic – including a sample which uses delegates to invoke a class’s instance and shared (static) methods.  Sandy also covered multicast delegates, which invoke multiple methods from a single call.  The power of delegates is that you can write code to call a single method, and then use delegates to have that one method call actually make calls to multiple methods.

Reading that sentence, it seems unclear.  So let’s try it with an example.  Suppose we have a program which deals with books.  We have two significant functions: PrintTitle and Totalizer.  If we create a delegate for each of those two functions, we can pass the pointer to the functions into a method call: ProcessBooks.  ProcessBooks with then either print the title of the book or perform the logic in the totalizer function, depending on which delegate was passed to it.  Notice that these two functions do nothing like each other – one handles strings and the other handles numbers.  The fact that the delegates for both have the same signature (number of parameters) means that you can use either delegate from the same method call.  Check back with the original post I wrote covering D’s presentation on delegates to the Mobility User Group.

— Matt Ranlett

posted with BlogJet

 

7/8/2005 1:33:40 PM (Eastern Standard Time, UTC-05:00)  #    Trackback
Tracked by:
"dangers of taking diet pills" (dangers of taking diet pills) [Trackback]