Reputation: 17373
Could anyone please explain what and how this code below is doing/working?
RoleEnvironment.Chaning += RoleEnvironmentChaning;
private void RoleEnvironmentChanaing(object sender, RoleEnvironmentchaningnEventArgs e)
{
......
}
basically, if you could walk me through how event handling works in c#.net will be greatly appreciated. Thanks.
Upvotes: 0
Views: 213
Reputation: 4886
Let's forget about C# for a second and think about the following scenario. You have a button on the screen that you want the user to click, you don't know when the user will click the button and neither do you want to check constantly if the user has clicked the button. What you want to do is run a bit of custom code when the user does eventually click on a button.
Welcome to events or delegates.
Let's take a look at the button. The Button has a Click event that you can hook your custom code onto. i.e.
//This happens in the designer
Button button = new Button();
button.Click += new EventHandler(YourMethod);
Your method will now be called once the button has been clicked.
What happens on Click of the button? Someone will check whether or not there are subscribers to the event
if(Click != null)
{
Click(this, someEventArguments);
}
Upvotes: 2
Reputation: 1501926
Basically that's saying: whenever the RoleEnviroment decided to trigger the "changing" event, call that method. (I assume it should be Changing rather than Chaning or Chanaing as per your code.)
In other words, events in C# are an implementation of the publisher/subscriber or observer pattern.
See my article on events and delegates for more information.
Upvotes: 2
Reputation: 11770
Some first page search results:
http://www.codeproject.com/KB/cs/csevents01.aspx
http://msdn.microsoft.com/en-us/library/aa645739%28VS.71%29.aspx
Upvotes: 0