?
Solved

C# code

Posted on 2011-04-25
2
Medium Priority
?
212 Views
Last Modified: 2012-05-11
Can someone tell me the meaning of add and remove here? I know that there are getter and setter properties, but what does these mean (is it a property?)?

public virtual event DataUpdateEventHandler DataChanged
            {
                  add
                  {
                        this.dataChangedEvent += value;
                        issueFiredEvents(value);
                  }
                  remove
                  {
                        this.dataChangedEvent -= value;
                  }
            }

-----------------------
////DataUpdateEventHandler is:-->
public delegate void DataUpdateEventHandler(object command, DataUpdateEventArgs eventInfo);
--------------------------
////and  DataUpdateEventArgs is inheriting from EventArgs.
public class DataUpdateEventArgs : EventArgs
0
Comment
Question by:vivekj2004
2 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35460722
The add/remove properties get fired when someone subscribes/unsubscribes from that event.  This allows you to take additional custom actions, such as running that issueFiedEvents() method.
0
 
LVL 10

Accepted Solution

by:
Asim Nazir earned 2000 total points
ID: 35464692
Hi,

Yes. You can say, they are properties but specific to Events implementation. Instead of Get and Set, for events C# has Add and Remove accessors.

As per MSDN:
The add contextual keyword is used to define a custom event accessor that is invoked when client code subscribes to your event. If you supply a custom add accessor, you must also supply a remove accessor.

The remove contextual keyword is used to define a custom event accessor that is invoked when client code unsubscribes from your event. If you supply a custom remove accessor, you must also supply an add accessor.

http://msdn.microsoft.com/en-us/library/cc713642.aspx
http://msdn.microsoft.com/en-us/library/cc713648.aspx

Nice discussion on the same:
http://bytes.com/topic/c-sharp/answers/813497-property-accessors-add-remove-event-properties-andeventhandlerlist

Using += you can added event to handler while using -= you are removing it. So if  you debug, you will get into EventHandling method soon after Event handler is assigned using += but it will not if it's removed i.e. using -=.

I hope it helps.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question