Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raise an event of a class from a different class in C#

Tags:

c#

events

I have a class, EventContainer.cs, which contains an event, say:

public event EventHandler AfterSearch; 

I have another class, EventRaiser.cs. How do I raise (and not handle) the above said event from this class?

The raised event will in turn call the handler of the event in the EventContainer class. Something like this (this is obviously not correct):

EventContainer obj = new EventContainer();  RaiseEvent(obj.AfterSearch); 
like image 447
samar Avatar asked Dec 07 '10 15:12

samar


2 Answers

This is not possible, Events can only be risen from inside the class. If you could do that, it would defeat the purpose of events (being able to rise status changes from inside the class). I think you are misunderstanding the function of events - an event is defined inside a class and others can subscribe to it by doing

obj.AfterSearch += handler; (where handler is a method according to the signature of AfterSearch). One is able to subscribe to the event from the outside just fine, but it can only be risen from inside the class defining it.

like image 125
Femaref Avatar answered Oct 14 '22 01:10

Femaref


It is POSSIBLE, but using clever hack.

Inspired by http://netpl.blogspot.com/2010/10/is-net-type-safe.html

If you don't believe, try this code.

using System; using System.Runtime.InteropServices;  namespace Overlapping {     [StructLayout(LayoutKind.Explicit)]     public class OverlapEvents     {         [FieldOffset(0)]         public Foo Source;          [FieldOffset(0)]         public OtherFoo Target;     }      public class Foo     {         public event EventHandler Clicked;          public override string ToString()         {             return "Hello Foo";         }          public void Click()         {             InvokeClicked(EventArgs.Empty);         }          private void InvokeClicked(EventArgs e)         {             var handler = Clicked;             if (handler != null)                 handler(this, e);         }     }      public class OtherFoo     {         public event EventHandler Clicked;          public override string ToString()         {             return "Hello OtherFoo";         }          public void Click2()         {             InvokeClicked(EventArgs.Empty);         }          private void InvokeClicked(EventArgs e)         {             var handler = Clicked;             if (handler != null)                 handler(this, e);         }          public void Clean()         {             Clicked = null;         }     }      class Test     {         public static void Test3()         {             var a = new Foo();             a.Clicked += AClicked;             a.Click();             var o = new OverlapEvents { Source = a };             o.Target.Click2();             o.Target.Clean();              o.Target.Click2();             a.Click();         }          static void AClicked(object sender, EventArgs e)         {             Console.WriteLine(sender.ToString());         }     } } 
like image 25
halorty Avatar answered Oct 14 '22 02:10

halorty