Delegates in VS .NET

Drawer Quick Response Code in VS .NET Delegates
Delegates
QR Code Drawer In .NET
Using Barcode printer for Visual Studio .NET Control to generate, create QR Code ISO/IEC18004 image in VS .NET applications.
Unfortunately, Peter was so busy talking his boss into implementing this interface that he didn't get around to notifying the universe, but he planned to get to that as soon as possible At least he'd abstracted the reference of his boss far away from him so that others who implemented the IWorkerEvents interface could be notified of his work progress Still, his boss complained bitterly "Peter!" his boss fumed "Why are you bothering to notify me when you start your work or when your work is progressing ! I don't care about those events Not only do you force me to implement those methods, but you're wasting valuable work time waiting for me to return from the event, which is further expanded when I am far away! Can't you figure out a way to stop bothering me " And so, Peter decided that even though interfaces were useful for many things, when it came to events, their granularity was not fine enough He wished to be able to notify interested parties only of the events that matched their hearts' desires Toward that end, Peter decided to break the methods out of the interface into separate delegate functions, each of which acted like a little tiny interface of one method each: delegate void WorkStarted(); delegate void WorkProgressing(); delegate int WorkCompleted(); class Worker {
Read Quick Response Code In .NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in VS .NET applications.
public void DoWork() { ConsoleWriteLine("Worker: work started"); if( started != null ) started(); ConsoleWriteLine("Worker: work progressing"); if( progressing != null ) progressing(); ConsoleWriteLine("Worker: work completed"); if( completed != null ) { int grade = completed(); ConsoleWriteLine("Worker grade= " + grade); } } public WorkStarted started; public WorkProgressing progressing; public WorkCompleted completed; } class Boss { public int WorkCompleted() { ConsoleWriteLine("Better"); return 4; /* out of 10 */ } } class Universe { static void Main() { Worker peter = new Worker(); Boss boss = new Boss(); // NOTE: We've replaced the Advise method with the // assignment operation petercompleted = new WorkCompleted(bossWorkCompleted); peterDoWork(); ConsoleWriteLine("Main: worker completed work"); ConsoleReadLine(); } }
Bar Code Generation In .NET Framework
Using Barcode generation for Visual Studio .NET Control to generate, create bar code image in VS .NET applications.
Static Subscribers
Barcode Reader In .NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
Delegates accomplished the goal of not bothering Peter's boss with events that he didn't want, but still Peter had not managed to get the universe on his list of subscribers Since the universe is an all-compassing entity, it didn't seem right to hook delegates to instance members (imagine how many resources multiple instances of the universe would need) Instead, Peter needed to hook delegates to static members, which delegates support fully: class Universe { static void WorkerStartedWork() { ConsoleWriteLine("Universe notices worker starting work"); } static int WorkerCompletedWork() { ConsoleWriteLine("Universe pleased with worker's work"); return 7; } static void Main() { Worker peter = new Worker(); Boss boss = new Boss();
Encoding QR Code 2d Barcode In C#.NET
Using Barcode generation for VS .NET Control to generate, create QR image in Visual Studio .NET applications.
// NOTE: the use of the assignment operator is *not* good // practice in the following 3 lines of code // Keep reading for the right way to add a delegate petercompleted = new WorkCompleted(bossWorkCompleted); peterstarted = new WorkStarted(UniverseWorkerStartedWork); petercompleted = new WorkCompleted(UniverseWorkerCompletedWork); peterDoWork(); ConsoleWriteLine("Main: worker completed work"); ConsoleReadLine(); } } [ Team LiB ]
QR Code ISO/IEC18004 Printer In .NET
Using Barcode maker for ASP.NET Control to generate, create QR Code 2d barcode image in ASP.NET applications.
[ Team LiB ]
QR Code 2d Barcode Creator In Visual Basic .NET
Using Barcode printer for .NET Control to generate, create QR Code 2d barcode image in Visual Studio .NET applications.
Events
Universal Product Code Version A Encoder In VS .NET
Using Barcode printer for Visual Studio .NET Control to generate, create Universal Product Code version A image in Visual Studio .NET applications.
Unfortunately, the universe, being very busy and unaccustomed to paying attention to individuals, had managed to replace Peter's boss's delegate with its own This was an unintended side effect of making the delegate fields public in Peter's Worker class Likewise, if Peter's boss got impatient, he could decide to fire Peter's delegates himself (which is just the kind of rude thing that Peter's boss was apt to do): // Peter's boss taking matters into his own hands if( petercompleted != null ) petercompleted(); Peter wanted to make sure that neither of these could happen He realized that he needed to add registration and unregistration functions for each delegate so that subscribers could add or remove themselves but couldn't clear the entire list or fire his events Instead of implementing these functions himself, Peter used the event keyword to make the C# compiler build these methods for him: class Worker { public event WorkStarted started; public event WorkProgressing progressing; public event WorkCompleted completed; } Peter knew that the event keyword erected a property around a delegate, allowing only clients to add or remove themselves (using the += and -= operators, if the client was written in C#), forcing his boss and the universe to play nicely: static void Main() { Worker peter = new Worker(); Boss boss = new Boss(); petercompleted += new WorkCompleted(bossWorkCompleted); peterstarted += new WorkStarted(UniverseWorkerStartedWork); petercompleted += new WorkCompleted(UniverseWorkerCompletedWork); peterDoWork(); ConsoleWriteLine("Main: worker completed work"); ConsoleReadLine(); }
Bar Code Printer In .NET Framework
Using Barcode generator for .NET framework Control to generate, create barcode image in Visual Studio .NET applications.
Bar Code Creation In .NET Framework
Using Barcode generation for .NET framework Control to generate, create barcode image in Visual Studio .NET applications.
Print Leitcode In VS .NET
Using Barcode generator for VS .NET Control to generate, create Leitcode image in VS .NET applications.
EAN13 Drawer In Java
Using Barcode generator for Java Control to generate, create EAN13 image in Java applications.
Decoding Code 128 Code Set A In .NET Framework
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
ECC200 Scanner In VS .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET applications.
Print Bar Code In C#
Using Barcode maker for .NET Control to generate, create bar code image in Visual Studio .NET applications.