I'm sure all of us have been there. Project requirements get clearly laid out. Software is developed. Then; when all is said and done...... "Hey; can you make your code do XXX?" Where 'XXX' completely messes up your code logic & complexity.
Recently, I learned about delegates in C# (I assume they exist in VB.NET) and how they help keep code clean and flexible. I've attached a very simple Form (from 9.01.01) I created that illustrates how they work.
[View:/cfs-file/__key/communityserver-discussions-components-files/78/DelegateExample.XML:320:240]
In the Form, I'm using the pre-defined delegate type Action. An Action takes up to 16 arguments and returns void. It's counter part is Func, where the last parameter in the call is the return value/type. An Action/Func is just a pointer to a method call.
I've created 3 classes: Email, Text and Phone. All inherit from FormScript so I am able to pass in IWS objects. Looking at my declaration, I used Action<string,IWSForm>. This tells the system "Hey, you can register any methods with me whose return type is void and takes a string and an IWSForm as parameters". In both cases, I register the email, text, and call methods with the handler. At the end of my Events, I make one single call to the NotificationHandler and all registered methods get called.
If management tells me "Hey; we don't want to send texts any more. We want to send smoke signals instead". All I would do is create my new class, SmokeSignals that has at least one method that takes <string,IWSForm> as parameters. I remove the registration of the text messages, change it to be my smoke signals call and poof: done.
I believe if the classes/methods are declared public static you won't have to create instances of the objects to get the method pointers.
I'm going to look into how I can use this to simplify both my Form code and my IDO Custom Assemblies code.
Has anyone used this in their code before? What have your experiences been?