How to write a custom event handler adder method in C# - c#

I have defined a Foo class and used this in Bar class, with a event handler:
public class Bar
{
public Bar()
{
Foo foo = new Foo();
foo.my_custom_event += my_event_handler;
}
public void my_event_handler()
{
// do work here
}
}
and this works perfectly. But I need to define a method in Foo class that will be fired when I add an event handler to my_custom_event, like:
public class Foo
{
...
public/private void my_event_handler_adder(target_function)
{
functions_that_are_fired_on_my_custom_event.Append(target_function);
}
}
Is there any way to define such an adder method?

class Foo
{
private EventHandler explicitEvent;
public event EventHandler ExplicitEvent
{
add
{
explicitEvent += value;
FireNeededMethodHere();
}
remove
{
explicitEvent -= value;
}
}
}

Related

How to raise the same event from different classes?

I have a class library that has a number of classes.
Any of these classes should be able to send a message (string) to the client at any point of time . I want to have a Generic Event that can be raised from a number of classes. I don't want a separate event for each class.
Something like this:
public class GenericEvent
{
// Here I have an event.
}
public class LibClass1
{
//Raise event here.
}
public class LibClass2
{
//Raise event here
}
public class Client
{
//Subscribe to the event here
}
Is this the right approach? If yes, how can it be achieved? The examples I looked up all have a separate event for each class.
It depends on what this event is and use cases, but one of the options is to use inheritance:
public class GenericEvent
{
// Here I have an event.
protected void RaiseEvent();
}
public class LibClass1 : GenericEvent
{
public voidDoSomethingAndRaiseEvent()
{
// ...
RaiseEvent();
}
}
This is how INotifiPropertyChanged is usually implemented.
If inheritance is impossible and you're using aggregation, LibClass1 and LibClass2 should act as some facade/decorator for GenericEvent: they must have their own event, which re-directs calls to GenericEvent's event and method(-s) to raise it:
public class GenericEvent
{
public event EventHandler SomeEvent;
// ...
}
public class LibClass1
{
private readonly GenericEvent _ge;
// ...
public event EventHandler SomeEvent
{
add { _ge.SomeEvent += value; }
remove { _ge.SomeEvent -= value; }
}
public void DoSomethingAndRaiseEvent()
{
// ...
SomeEvent?.Invoke(this, EventArgs.Emtpy);
}
}
public class MyEventArgs : EventArgs
{
// class members
}
public abstract class Lib
{
public event EventHandler ShapeChanged;
public virtual void OnShapeChanged(MyEventArgs e)
{
if (ShapeChanged != null)
{
ShapeChanged(this, e);
}
}
}
public class LibClass1 : Lib
{
//Raise event here.
}
public class LibClass2 : Lib
{
//Raise event here
}
static void Main(string[] args)
{
LibClass1 lib1 = new LibClass1();
LibClass2 lib2 = new LibClass2();
lib1.ShapeChanged += Lib1_ShapeChanged;
lib2.ShapeChanged += Lib1_ShapeChanged;
lib1.OnShapeChanged(new MyEventArgs());
}
Here full example create an abstract class in which you have the event.
I would work with inheritance. For example:
public class ParentClass : Form
{
public ParentClass() {
this.FormClosed += sendString;
}
private void sendString(object sender, FormClosedEventArgs e)
{
throw new NotImplementedException();
}
}
public class GenericEvent : ParentClass { }
public class LibClass1 : ParentClass { }
public class LibClass2 : ParentClass { }
public class Client : ParentClass { }
Now all of you Clases have the event of the ParentClass.
I have another approach.
Derive all of your classes from one single base class. (of course any library do that, .net or MFC or Qt or java framework).
you have a single event "event 1" in base class. In that event1 handler, raise "event 2".
Subscribe all your child classes to the "event2" of parent class and handle your business in respective child classes.

Event must be of delegate type?

Not very familiar with declaring and using events and received error,
Event must be of delegate type
Basically want to pass IMyInterface as a dependency to another class where that class can subscribe to receive MyClassEvent events and the event data is MyClass.
public interface IMyInterface
{
event MyClass MyClassEvent;
}
public class Implementation: IMyInterface
{
event MyClass MyClassEvent;
public void OnSomethingHappened
{
MyClassEvent?.Invoke(); // pass MyClass to subscribers
}
}
public class AnotherClass(IMyInterface ...)
{
OnMyClassEvent(MyClass args)
{
// do something
}
}
You need to declare the event correctly and define the event args:
public class MyClassEventArgs : EventArgs { }
public interface IMyInterface
{
event EventHandler<MyClassEventArgs> MyClassEvent;
}
public class Implementation : IMyInterface
{
public event EventHandler<MyClassEventArgs> MyClassEvent;
public void OnSomethingHappened()
{
MyClassEvent?.Invoke(this, new MyClassEventArgs());
}
}
And to subscribe to it:
var implementation = new Implementation();
implementation.MyClassEvent += MyClassEvent;
private void MyClassEvent(object sender, MyClassEventArgs e) { ... }

Passing EventHandler between several classes

I am creating a gui widget a dll library, a class that derives from a FORM (I thought about usercontrol but it doesn't have built in properties like Opacity and some more that i need).
Because I don't want to expose to the user of this control all the standard methods and propertis that this derived class will inherit, I've created a nothe class "middle-man" that should encapsulate and expose only the needed methos to the user of this dll.
The problem is exposing the events.
This is an abstract example:
class Class1
{
Class2 theClass2;
public Class1()
{
theClass2 = new Class2();
theClass2. += new EventHandler(theClass3_EventHandler);
theClass2.TriggerEvent();
}
void theClass3_EventHandler(object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
class Class2
{
Class3 theClass3;
public Class2()
{
theClass3 = new Class3();
}
public void TriggerEvent()
{
theClass3.Trigger();
}
class Class3
{
public event EventHandler theEvent;
public void Trigger()
{
if (this.theEvent != null)
theEvent(this, new EventArgs());
}
}
}
public event EventHandler theEvent {
add {
SomeNestedClass.theEvent += value;
}
remove {
SomeNestedClass.theEvent -= value;
}
}
First of all, inheritance is a is-a relationship. That means that everything that a Form can do should all sub classes be able to do. Since this is not the case for you, you should not derive the form.
As for the events. You need to redefine them in your "middle-man" class. And in that class simply subscribe on the events from the form and trigger them in the handler methods.
public class MyCoolControl : Control
{
private Form _customForm;
public MyCoonControl()
{
_customForm.Clicked += (source, e) => Clicked(source,e);
}
public event EventHandler Clicked = delegate {};
}

Add event to another event? c#

class a
{
public event Action foo;
var zzz = new b();
foo += zzz.bar;
}
class b
{
public Action bar;
}
The above (pseudo) code works and compiles fine.
However, if i change bar to public event Action bar I cant then add it to foo.
Basically I would like to add one event to another. Im aware this could sound abit ridiculous.
IIRC you can't invoke events from another class directly.
class A {
public A() {
b = new B(this);
}
private B b;
public event Action Foo;
}
class B {
public B(A a) {
a.Foo += InvokeBar;
}
public event Action Bar;
private void InvokeBar() {
if (Bar != null)
Bar();
}
}
What do you want to achieve is something like this (I guess):
foo event is triggered:
call all foo subscribed event handlers plus all bar subscribed event handlers.
bar is triggered:
call all bar subscribed event handlers plus all foo subscribed event handlers.
class a
{
public event Action foo;
b zzz = new b();
public a()
{
// this allow you to achieve point (1)
foo += zzz.FireBarEvent;
// this allow you to achieve point (2)
zzz.bar += OnBar;
}
void OnBar()
{
FireFooEvent();
}
void FireFooEvent()
{
if(foo != null)
foo();
}
}
class b
{
public event Action bar;
public void FireBarEvent()
{
if(bar != null)
bar();
}
}
CAVEAT:
this code (if (1) and (2) options are both enabled) cause an infinite numbers of calls i.e.:
foo --> bar --> foo --> bar ...
that has to be managed properly.
If bar is a public event, then you use a lambda to invoke the bar event:
foo += () => zzz.bar();
This is not the exact syntax, researching...
This is not possible, because you cannot call the bar event from outside the class it is defined in.
You should use a solution like this;
class b {
public Action bar;
public void InvokeBar() {
if (bar != null) bar();
}
}
Then you can use InvokeBar as a target for your event.

Events + Adapter Pattern

I have an adapter pattern on a generic class that essentially adapts between types:
class A<T> { event EventHandler e; }
class Aadapter<T1, T2> : A<T1> { A<T2> a; Aadapter(A<T2> _a) { a = _a; } }
The problem is that A contains an event. I effectively want all event handlers assigned to Adapter to fall through to a.
It would be awesome if I could assign the a's event handler to adapter's event handler but this is impossible?
The idea here is that A is almost really just A but we need a way to adapt the them. Because of the way event's work I can't how to efficiently do it except manually add two event handlers and when they are called they "relay" the to the other event. This isn't pretty though and it would seem much nicer if I could have something like
class A<T> { event EventHandler e; }
class Aadapter<T1, T2> : A<T1> { event *e; A<T2> a; Aadapter(A<T2> _a) { a = _a; e = a.e; } }
in a sense we have a pointer to the event that we can assign a2's event to.
I doubt there is any simple way but maybe someone has some idea to make it work.
(BTW, I realize this is possible with virtual events but I'd like to avoid this if at all possible)
I think that this is what you are after:
class A<T>
{
public virtual event EventHandler e;
}
class Aadapter<T1, T2> : A<T1>
{
A<T2> a;
Aadapter(A<T2> _a) { a = _a; }
public override event EventHandler e
{
add { a.e += value; }
remove { a.e -= value; }
}
}
Or chain it
class A<T>
{
public event EventHandler e;
protected void ChainEvent(object sender, EventArgs eventArgs)
{
e(sender, eventArgs);
}
}
class Aadapter<T1, T2> : A<T1>
{
A<T2> a;
Aadapter(A<T2> _a)
{
a = _a;
a.e += ChainEvent;
}
}
Why is subscribing and forwarding events not pretty? I find it elegant.
Doing this is consistent with the way the rest of the adapter is implemented.
Even if you could use a pointer, it would be inconsistent because you wouldn't want to do that in every case.
For example, if you're adapting a class that implements INotifyPropertyChanged to an interface that does not, but exposes a couple of properties, such as "TitleChanged" and "MaxLengthChanged," you wouldn't then use a pointer. Your adapter would expose those two events, and consumers would subscribe. Your adapter would subscribe to the PropertyChanged event, and raise "TitleChanged" only when it gets notified that "Title" was modified, and "MaxLengthChanged" only when it gets notified that "MaxLength" was modified. All other notifications would be ignored.
I favour this approach as I find it straightforward, consistent and true to the pattern.
Example showing the "standard" methods to solve the problem. The first uses virtual events/methods while the second does a "doubled ended" forwarding scheme. Both have their pro's and cons but would be nice if there was an easier method that didn't grow with the number of events. What we would like to do is sort of combine the two events into one directly instead of indirectly which is what all this code does. (pointers would be such a method if they were possible in C#)
//#define __virtual
#define __direct
using System;
using System.Collections.Generic;
using System.Text;
namespace VirtualEvents
{
#if __virtual
#region
public class A<T>
{
public virtual event EventHandler e;
public virtual void Fire() { e(this, null); }
}
public class Aadapter<T1, T2> : A<T1>
{
A<T2> a;
public override event EventHandler e
{
add { a.e += new EventHandler(value); }
remove { a.e -= new EventHandler(value); }
}
public override void Fire()
{
a.Fire();
}
public Aadapter(A<T2> _a)
{
a = _a;
}
}
#endregion
#elif __direct
#region
public delegate EventHandler EventHandlerPtr();
public class eventPtr
{
public EventHandler _event;
}
public class A<T>
{
//internal EventHandler _event;
public eventPtr _event = new eventPtr();
public event EventHandler e
{
add { _event._event += value; }
remove { _event._event -= value; }
}
public void Fire() { _event._event(this, null); }
}
public class Aadapter<T1, T2> : A<T1>
{
A<T2> a;
public Aadapter(A<T2> _a)
{
a = _a;
this._event = a._event;
}
}
#endregion
#else
#region
public class A<T>
{
public event EventHandler e;
public void Fire() { e(this, null); }
}
public class Aadapter<T1, T2> : A<T1>
{
A<T2> a;
public Aadapter(A<T2> _a)
{
a = _a;
a.e += new EventHandler(a_e);
e += new EventHandler(Aadapter_e);
}
void Aadapter_e(object sender, EventArgs e)
{
a.e -= new EventHandler(a_e);
a.Fire();
a.e += new EventHandler(a_e);
}
void a_e(object sender, EventArgs e)
{
this.e -= new EventHandler(Aadapter_e);
Fire();
this.e += new EventHandler(Aadapter_e);
}
}
#endregion
#endif
class Program
{
static void Main(string[] args)
{
var a = new A<double>();
var q = new Aadapter<int, double>(a);
a.e += new EventHandler(a_e);
q.e += new EventHandler(q_e);
a.Fire();
q.Fire();
((A<int>)q).Fire();
Console.ReadKey();
}
static void a_e(object sender, EventArgs e)
{
Console.WriteLine("From a");
}
static void q_e(object sender, EventArgs e)
{
Console.WriteLine("From q");
}
}
}
(edit: the code now includes a new method which wraps the event in a class which now allows events to be assigned easily and effectively represents the "the pointer" case. Hopefully someone can improve upon these even more.)

Categories