.

delegates and events

steps:

  1. declare delegate
    • delegate [result-type] [delegateName] ([parameters]);
  2. declare event based on delegate
    • public event [delegateName] [EventName];
  3. fire the event
    • if ([EventName] != null)
      { [EventName]([arguments]); }

 

example:

 

public delegate void RowButtonClickHandler (object sender, int row);

public event  RowButtonClickHandler RowClick;

if (RowClick != null)
{
     int rownum = 5; //example only   
     RowClick (this, rownum);    
}

 

 

 

 

another example:

 

public delegate void DataSelectedHandler(object sender, DataSelectedArgs e);
    public event DataSelectedHandler OnDataSelected;
    public class DataSelectedArgs : EventArgs
    {
        public datacontainer DC;
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index =  GridView1.SelectedIndex;
        datacontainer dc = dcl[index];
        if (OnDataSelected != null)
        {
            DataSelectedArgs eventArgs = new DataSelectedArgs();
            eventArgs.DC = dc;
            OnDataSelected(this, eventArgs);   
        }
    }

 

 

COPY & PASTE The following into your class as a template:

#region EVENTS
        public class DataSelectedArgs : EventArgs
        {
            public string setting;
        }
        public delegate void DataSelectedHandler(object sender, DataSelectedArgs e);
        public event DataSelectedHandler OnDataSelected;
        public void FireEvent()
        {
            if (OnDataSelected != null)
            {
                OnDataSelected(this, new DataSelectedArgs() { setting = "wer" });
            }
        }
        #endregion

What's your thoughts on this?

*

Protected by WP Anti Spam