954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

event & delegate

Hi :)
i am having a hard time understanding the concept of event & delegate.

i think i understand that one object start an event and some other objects can react while the event start.

but why is that different from the observer design pattern?
or how is it different from get/set properties settings? (like set that when the X property of an object set something happened in other objects)

can someone answer those questions?
and if u have a simple example of implementation of that issue ill be glad to read it

thanks!!! :)

galhajaj
Light Poster
36 posts since May 2011
Reputation Points: 13
Solved Threads: 0
 

A delegate is the .Net equivalent of a function pointer. Basically it describes a function/method to be called (return type, parameters count, and parameter types) without actually being a function - sort of a prototype of a function/method. These allow your code to be able to describe a function without actually knowing what the function's name is or what it does. All that it needs is the same return type, parameter count, and parameter types. This is similar to what an interface does for classes, only it's an 'interface' for methods/functions.

An Event is a special type of delegate that allows object(s) to subscribe to it. When a condition is met, the event is called and the method executed. EventHandlers are used to subscribe to events - an event can have unlimited subscribers, and an EventHandler can subscribe to unlimited events. I can't really explain this much better than any event-driven programming .Net tutorial off google could do, unfortunately.

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

Here is an example of calling a delegate (from a console app) without invoking an event handler.
This example shows a call to the method ProcessSomething.
There are two instances where delegates are in play
1) Where todaysMethod is set
2) Where ProcessSomething() calls the display method

using System;
using System.Collections.Generic;
using System.IO;

namespace DW_404922_CS_CON
{
   class Program
   {
      private static void DoOutputToTempFile(string strData)
      {
         string strTempFileName = Path.GetTempFileName();
         StreamWriter fileOut = new StreamWriter(strTempFileName);
         fileOut.WriteLine(strData);
         fileOut.Close();
         File.Delete(strTempFileName);
      }

      private static void DoOutputToScreen(string strData)
      {
         Console.WriteLine(strData);
      }

      private static void ProcessSomething(Action<string> doSomething, string strData)
      {
         doSomething(strData);
      }

      static void Main(string[] args)
      {
         List<string> lst_strData = new List<string>()
         {
            "once upon a time",
            "four-score and seven years ago",
            "there once was a man from Peru",
            "the only thing we have to fear is fear itself",
            "give me liberty or give me death",
         };

         Action<string> todaysMethod = DoOutputToScreen;

         lst_strData.ForEach(s => ProcessSomething(todaysMethod, s));
      }
   }
}
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: