Saturday, February 18, 2012

Event c#

using System;

public delegate void DivBySevenHandler(object o, DivBySevenEventArgs e);

public class DivBySevenEventArgs : EventArgs
{
    public readonly int TheNumber;
   
    public DivBySevenEventArgs(int num)
    {
        TheNumber = num;
    }   
   
}

public class DivBySevenListener
{
    public void ShowOnScreen(object o, DivBySevenEventArgs e)
    {
        Console.WriteLine(
            "divisible by seven event raised!!! the guilty party is {0}",
            e.TheNumber);
    }   
}

public class BusterBoy
{
    public static event DivBySevenHandler EventSeven;
   
    public static void Main()
    {
        DivBySevenListener dbsl = new DivBySevenListener();
        EventSeven += new DivBySevenHandler(dbsl.ShowOnScreen);
        GenNumbers();
    }
   
    public static void OnEventSeven(DivBySevenEventArgs e)
    {
        if(EventSeven!=null)
            EventSeven(new object(),e);
    }   
   
    public static void GenNumbers()
    {
        for(int i=0;i<99;i++)
        {
            if(i%7==0)
            {
                DivBySevenEventArgs e1 = new DivBySevenEventArgs(i);
                OnEventSeven(e1);
            }
        }       
    }
       
}
-------------------------------------------------------------------------
  1. Event handler is something defined in a class, like the sample you gave me. You can think of an Event handler as a type, like a class or a primitive type.
  2. Class Skiepage
    {
           …
    public
    …     
    EventHandler<EventArgs> SelectedObjectChanged;Skiepage skiepage = new Skiepage();SelectedObjectChanged += EventConsumer_selectChanged;private void EventConsumer_selectChanged(object o, EventArgs e)
}
When you consume an event handler, you usually register a method to that event handler
Class EventComumer
{
      
       skiepage.
             
              {
                     Do something;
              }
}
Notice that how an EventHandler(SelectedObjectChanged) is defined in a class and how to attach a method to an event handler

1 comment:

  1. In case you are interested in making cash from your websites or blogs by popup advertisments - you can use one of the highest paying networks - Clicksor.

    ReplyDelete