473,320 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

event handlers

hi i am looking at a colleagues code and i fail to understand what these
mean.

public event DataListItemEventHandler ItemSelect
{
add
{
this.Events.AddHandler(EventItemSelect, value);
}
remove
{
this.Events.RemoveHandler(EventItemSelect, value);
}
}

/// <summary>
/// Fired when data list item is selected
/// </summary>
public event DataListItemEventHandler ItemSelected
{
add
{
this.Events.AddHandler(EventItemSelected, value);
}
remove
{
this.Events.RemoveHandler(EventItemSelected, value);
}
}

i am aware of events but what is this add and remove any small example about
these would be great
Nov 16 '05 #1
2 1554
Vas
Hi, I think your friend is trying a "Source and Receiver" thingy. The code looks a bit dodgy though

Basically he/she is binding the source to the receiver, if you want to try it for yourself, try

1. Create the source class and delegate

// delegate the RECEIVER has to implemen
public delegate void AnEventHandler(string msg)

// the source clas
public class Sourc

// event handle
public event AnEventHandler OnAnEventHandler

// method to raise the even
public void RaiseEvent(string msg

OnAnEventHandler(msg); // Fir

2. Create the receiver to listen and act on the event

// the receiver clas
public class Receive

// The receiver has to know about the event sourc
Source theSource

public Receiver(Source src

// bind the sourc
theSource = src
// event handle
private void EventMessage(string msg

Console.WriteLine("Message is {0}", msg)
// subscribe to the even
public void AddEvent(

theSource.OnAnEventHandler += new AnEventHandler(EventMessage)
// unsubscribe from the even
public void RemoveEvent(

theSource.OnAnEventHandler -= new AnEventHandler(EventMessage)

3. Next, test it

static void Main(string[] args

// create a sourc
Source src = new Source()

// create a receiver and bind it to the sourc
Receiver rcv = new Receiver(src)

// subscribe to even
// receiver is ready and listening for event
rcv.AddEvent()

// raise even
src.RaiseEvent("Hello all!!!")

// unsubscribe from even
rcv.RemoveEvent()
}
Nov 16 '05 #2
frazer <ic***@hotmail.com> wrote:
hi i am looking at a colleagues code and i fail to understand what these
mean.
<snip>
i am aware of events but what is this add and remove any small example about
these would be great


See http://www.jaggersoft.com/csharp_standard/17.7.1.htm

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
8
by: Dennis C. Drumm | last post by:
I have a class derived from a SortedList called SystemList that contains a list of objects indexed with a string value. The definition of the objects contained in the SortedList have a boolean...
18
by: bhavin | last post by:
Hi, Can someone point me to some good best practices kind of documentation on use of events compared to method overriding. Ex. In Windows Forms when should i have an event handler for Paint, and...
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
5
by: Amit | last post by:
Hello! Is it possible to find how many event handlers an event has at runtime? How about finding whether or not an event has event handlers? In C# you can compare the event with null to check if...
6
by: Kevin Attard | last post by:
I am using a GridView inside a UserControl which has a template column for deleting the rows. Before databinding the gridview i am attaching the RowCommand and RowDataBound event. I am using the...
16
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some...
14
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event...
6
by: Jake K | last post by:
Where is the best place to define event handlers? I need to define event handlers for a COM object referenced in my project and am doing so in the fowm load. Is this the best place?
6
by: Tony Johansson | last post by:
Hello! I know it's possible to have several event handler for a single event but I can't see any point using several event handler for this. I'm probably wrong so can anybody tell me what...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.