{"id":747,"date":"2010-02-19T11:02:15","date_gmt":"2010-02-19T11:02:15","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=747"},"modified":"2010-02-22T20:46:38","modified_gmt":"2010-02-22T20:46:38","slug":"events-fire-that-event","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/02\/19\/events-fire-that-event\/","title":{"rendered":"Events &#8211; fire that event"},"content":{"rendered":"<p>An event will be fired when something has happened and then you can link to that event and do something else if you wanted to.  To start of with you have to link a event with a <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/01\/18\/delegates-csharp\/\">delegate<\/a> function which will be the event handler.<\/p>\n<p>To setup a delegate you can have the object and also any event arguments ( you can add in more if you want to, but these are the basics to include)<\/p>\n<pre lang=\"csharp\">\r\n\t\/\/ setup a delegate for the event to fire with.\r\n\tpublic delegate void ChangedEventHandler(object sender, EventArgs e);\r\n<\/pre>\n<p>here is the class that will actual fire the event, to start with I link the &#8220;event&#8221; to a virtual method as such called Changed (with the delegate from above ChangedEventHander).<\/p>\n<pre lang=\"csharp\">\r\n\t\/\/ this is the MyEvent class that will create the event fired.\r\n\tpublic class MyEvent \r\n\t{\r\n\t\tprivate int x;\r\n\t\t\r\n\t\t\/\/ the event for this internal class\r\n\t\tpublic event ChangedEventHandler Changed;\r\n\t\t\r\n<\/pre>\n<p>here is the event firing function, this will call the Changed &#8220;virtual&#8221; method as such when ever you call this function.<\/p>\n<pre lang=\"csharp\">\r\n\t\tprotected virtual void ThisHasChanged(EventArgs e)\r\n\t\t{\r\n\t\t\t\/\/ try and call the delegate ChangedEventHandler\r\n\t\t\t\/\/ make sure that there is something to call for the event to fire.\r\n\t\t\tif (Changed != null)\r\n\t\t\t\tChanged(this, e);\r\n\t\t}\r\n\t\t\r\n<\/pre>\n<p>and here is the get\/set for the internal X variable and in the set I call the ThisHasChanged function above.<\/p>\n<pre lang=\"csharp\">\r\n\t\t\/\/ call the ThisHasChanged event when you set the internal value of X.\r\n\t\tpublic int xValue\r\n\t\t{\r\n\t\t\tget { return x;}\r\n\t\t\tset { x = value;ThisHasChanged(EventArgs.Empty); }\r\n\t\t}\r\n\t}\r\n<\/pre>\n<p>So that is the event firing, but you will need to have a listener that will listen to any events and call something that will deal with the fired event, so here is the listener class.<\/p>\n<p>So the listener, is just like any other class, but you will need to have a internal reference to the class above so that you can link to the event fired, so here is the MyEvent internal class for the listener.<\/p>\n<pre lang=\"csharp\">\r\n\t\/\/ the listener for the event firing\r\n\tclass EventListener\r\n\t{\r\n\t\t\/\/ MyEvent class\r\n\t\tprivate MyEvent ThisIsMyEvent;\r\n<\/pre>\n<p>and then the constructor for the EventListener, you pass in the MyEvent class that you want to &#8220;link&#8221; (the one that you will create within the running part of the program).  and from within the MyEvent, there was a &#8220;virtual&#8221; method as such called &#8220;Changed&#8221;, well now we can create a link so that when that event &#8220;Changed&#8221; has been called from within the MyEvent Class I can link to a function within this class EventListener with using a new delegate ChangedEventHandler and the ValueHadChanged is within the EventListener class.<\/p>\n<pre lang=\"csharp\">\t\t\r\n\t\t\/\/ the class constructor and you need to pass the MyEvent class to link to the listener.\r\n\t\tpublic EventListener(MyEvent SetUpTheEventLink)\r\n\t\t{\r\n\t\t\t\/\/ Create a link to the MyEvent class passed in to the internal MyEvent Class\r\n\t\t\tThisIsMyEvent = SetUpTheEventLink;\r\n\t\t\t\/\/ this is the link to the listern function.\r\n\t\t\tThisIsMyEvent.Changed += new ChangedEventHandler(ValueHadChange);\r\n\t\t}\r\n<\/pre>\n<p>here is the ValueHadChange, same setup as the delegate parameters since we are basically passing event details between them both and can pass in more details is you want to, but you really only want to have the object (the MyEvent class) and any event arguments e.g. errors etc.  at present it just says , &#8220;Oh yeah.. I have altered&#8221;.<\/p>\n<pre lang=\"csharp\">\t\t\r\n\t\t\/\/ same style as the delegate e.g. object, eventargs, that is sent from the event fired.\r\n\t\t\/\/ THIS IS THE LISTERN as such, function..if you want to do something like.. do not alter the value!! \r\n\t\tprivate void ValueHadChange(object sender, EventArgs e)\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"Oh yeah.. I have been altered\");\r\n\t\t}\r\n<\/pre>\n<p>since there was a Changed link attached to the above function, to clean up you can detach the link with using something similar to the constructor of this class, but instead of using += (to add) you just need to subtract from the Change virtual method -=<\/p>\n<pre lang=\"csharp\">\r\n\t\t\/\/ detach the listener function from the event MyEvent \r\n\t\tpublic void Detach()\r\n\t\t{\r\n\t\t\t\/\/ take the event linked from the MyEvent class.\r\n\t\t\tThisIsMyEvent.Changed -= new ChangedEventHandler(ValueHadChange);\r\n\t\t\tThisIsMyEvent = null;\r\n\t\t}\r\n\t}\r\n<\/pre>\n<p>Here is the full code<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\n\r\n\/\/ when alter a number in the internal part of the class.\r\nnamespace codingfriendsEvents\r\n{\r\n\t\/\/ setup a delegate for the event to fire with.\r\n\tpublic delegate void ChangedEventHandler(object sender, EventArgs e);\r\n\t\r\n\t\/\/ this is the MyEvent class that will create the event fired.\r\n\tpublic class MyEvent \r\n\t{\r\n\t\t\/\/ the event for this internal class\r\n\t\tpublic event ChangedEventHandler Changed;\r\n\t\t\r\n\t\tprivate int x;\r\n\t\t\r\n\t\tprotected virtual void ThisHasChanged(EventArgs e)\r\n\t\t{\r\n\t\t\t\/\/ try and call the delegate ChangedEventHandler\r\n\t\t\t\/\/ make sure that there is something to call for the event to fire.\r\n\t\t\tif (Changed != null)\r\n\t\t\t\tChanged(this, e);\r\n\t\t}\r\n\t\t\r\n\t\t\/\/ call the ThisHasChanged event when you set the internal value of X.\r\n\t\tpublic int xValue\r\n\t\t{\r\n\t\t\tget { return x;}\r\n\t\t\tset { x = value;ThisHasChanged(EventArgs.Empty); }\r\n\t\t}\r\n\t}\r\n\t\r\n\t\/\/ the listener for the event firing\r\n\tclass EventListener\r\n\t{\r\n\t\t\/\/ MyEvent class\r\n\t\tprivate MyEvent ThisIsMyEvent;\r\n\t\t\r\n\t\t\/\/ same style as the delegate e.g. object, eventargs, that is sent from the event fired.\r\n\t\t\/\/ THIS IS THE LISTERN as such, function..if you want to do something like.. do not alter the value!! \r\n\t\tprivate void ValueHadChange(object sender, EventArgs e)\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"Oh yeah.. I have been altered\");\r\n\t\t}\r\n\r\n\t\t\/\/ the class constructor and you need to pass the MyEvent class to link to the listener.\r\n\t\tpublic EventListener(MyEvent SetUpTheEventLink)\r\n\t\t{\r\n\t\t\t\/\/ Create a link to the MyEvent class passed in to the internal MyEvent Class\r\n\t\t\tThisIsMyEvent = SetUpTheEventLink;\r\n\t\t\t\/\/ this is the link to the listern function.\r\n\t\t\tThisIsMyEvent.Changed += new ChangedEventHandler(ValueHadChange);\r\n\t\t}\r\n\t\t\r\n\t\t\/\/ detach the listener function from the event MyEvent \r\n\t\tpublic void Detach()\r\n\t\t{\r\n\t\t\t\/\/ take the event linked from the MyEvent class.\r\n\t\t\tThisIsMyEvent.Changed -= new ChangedEventHandler(ValueHadChange);\r\n\t\t\tThisIsMyEvent = null;\r\n\t\t}\r\n\t}\r\n\t\r\n\tclass TestMyEvent\r\n\t{\r\n\t\tpublic static void Main()\r\n\t\t{\r\n\t\t\t\/\/ create a new MyEvent class\r\n\t\t\tMyEvent MyEventToCheck = new MyEvent();\r\n\t\t\t\r\n\t\t\t\/\/ link in with the EventLister , passing in the class of MyEvent\r\n\t\t\tEventListener TheListener = new EventListener(MyEventToCheck);\r\n\t\t\t\r\n\t\t\t\/\/ should fire of a event !! because I am changing the internal x value.\r\n\t\t\tMyEventToCheck.xValue = 10;\r\n\t\t\t\r\n\t\t\t\/\/ will not fire a event because I have not changed it.\r\n\t\t\tConsole.WriteLine(\"X = \" + MyEventToCheck.xValue);\r\n\t\t\t\r\n\t\t\t\/\/ will fire of another event.\r\n\t\t\tMyEventToCheck.xValue = 5;\r\n\t\t\t\r\n\t\t\tTheListener.Detach();\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>and here would be the output<\/p>\n<pre lang=\"bash\">\r\nOh yeah.. I have been altered\r\nX = 10\r\nOh yeah.. I have been altered\r\n<\/pre>\n<p>If you want to gain access to the object sender within the EventListener class to the function that was listening to the event fired, if you alter it as below, but also since because the MyEvent was also linked within the constructor you also have that link as well.<\/p>\n<pre lang=\"csharp\">\r\n\t\t\/\/ same style as the delegate e.g. object, eventargs, that is sent from the event fired.\r\n\t\t\/\/ THIS IS THE LISTERN as such, function..if you want to do something like.. do not alter the value!! \r\n\t\tprivate void ValueHadChange(object sender, EventArgs e)\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"Oh yeah.. I have been altered\");\r\n\t\t\tConsole.WriteLine(\"New value of X = \" + ((MyEvent)sender).xValue);\r\n\t\t\tConsole.WriteLine(\"Internal MyEvent class x = \" + ThisIsMyEvent.xValue);\r\n\t\t}\r\n<\/pre>\n<p>then you can gain access to the MyEvent from within the object sender and the output would be<\/p>\n<pre lang=\"bash\">\r\nOh yeah.. I have been altered\r\nNew value of X = 10\r\nInternal MyEvent class x = 10\r\nX = 10\r\nOh yeah.. I have been altered\r\nNew value of X = 5\r\nInternal MyEvent class x = 5\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>An event will be fired when something has happened and then you can link to that event and do something else if you wanted to. To start of with you have to link a event with a delegate function which will be the event handler. To setup a delegate you can have the object and &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/02\/19\/events-fire-that-event\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Events &#8211; fire that event<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[106,105],"class_list":["post-747","post","type-post","status-publish","format-standard","hentry","category-c_sharp","tag-delegates","tag-events"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=747"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/747\/revisions"}],"predecessor-version":[{"id":755,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/747\/revisions\/755"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}