public Class A
{
public A()
{
someotherclass.someevent += new EventHandler(HandleEvent);
}
private void HandleEvent(object sender,CustomEventArgs e)
{
if(e.Name == "Type1")
Method1();
else if(e.Name == "Type2")
Method2();
}
protected virtual void Method1(){}
protected virtual void Method2(){}
}
public class B: A
{
public B()
{ /*Something*/}
protected override void Method1(){/*some logic*/}
protected override void Method2(){/*some other logic*/}
}
public class C: A
{
public C()
{ /*Something*/}
protected override void Method1(){/*some logic*/}
protected override void Method2(){/*some other logic*/}
}
public class Main
{
private A;
public Main(){/*Something*/}
private void StartB()
{
A = new B();
}
private void StartC()
{
A = new C();
}
}
Now, what happens is, after I go through a cycle in which both the methods StartB(called first) and StartC(called second) are called, when the someevent is triggered, the code tries to execute the Method in Class B(and later Class C, I hope. I could not get there since it errors out when it calls method in Class B), instead which I want it to call only the method in Class C.
I think that, since the event is subscribed at constructor, Class B methods are still getting fired since it is subscribed initially on the call of StartB.
Question:
I want only the methods of the class that is instantiated the latest should be executed.
For Example: if StartB and StartC are called in order, when someevent is triggered the Methods in Class C should only get executed. Same Vice-Versa. How to do that?
I know am doing something terribly wrong. Any help is much appreciated.
You aren't unsubscribing from the event from your first instance so it will be called. If you don't want it to be called you need to unsubscribe. You could do something like this
class A
{
private static EventHandler lastHandler;
public A()
{
//warning, not thread safe
if(lastHandler != null)
{
someotherclass.someevent -= lastHandler;
}
lastHandler = new EventHandler(HandleEvent);
someotherclass.someevent += lastHandler;
}
but it seems pretty hacky. You are probably better off implementing a method (e.g. IDisposable) to clean up your last instance before a creating a new one.
If I understand you correctly you are saying the methods on B are being called after startC is called and you don't wish this to happen?
I'm guessing your issue is that someotherclass is a static class, or an instance is somehow being shared between all the created B's and C's - in which case you need to unregister the old event handler from someotherclass.someevent when you create the new class. If you don't unregister the handler then the someotherclass object will have a reference to the B or C object that registered with it, so even though you are overwriting the reference in the main class the object is still kept alive by the reference in the event and is still being called when the event is triggered.
Related
I think my question is best descirbed by a code snippet:
class A
{
public void FunctionToBeCalled();
}
class B
{
public void FunctionToBeCalledAfter();
}
Now, after a FunctionToBeCalledAfter() call, FunctionToBeCalled() needs to "know" it must be called. B cannot have an A member, but A can have a B member. Is there any way this can be implemented in C#?
Why i need this:
Class A is Application level on OSI stack. Classes B and C(unmentioned before) are Transport Level. C makes calls to FunctionToBeCalledAfter, and after this FunctionToBeCalled needs to be called. But sincer A is a higher level, B and C cannot depend(have a member A), i don't know how to call FunctionToBeCalled.
I see 2 ways to accomplish this, one easier but (arguably) less elegant, one a little more involved but (arguably) more elegant
The less elegant solution: Singleton
A Singleton pattern enforces that there can only ever be one instance of a class at any given time, this seems to line up with your description of A (which from here on out I'll call Foo, and I'll be calling B Bar). So let's implement it:
public class Foo
{
private static Foo _instance;
public static Foo Instance => _instance ?? (_instance = new Foo());
// Private constructor so no one else can instantiate Foo
private Foo() { }
public void FunctionToBeCalled() { /* your code here */ }
}
public class Bar
{
public void FunctionToBeCalledAfter()
{
// Your existing code here
Foo.Instance.FunctionToBeCalled();
}
}
Now, the problem here is if your requirements ever change and you need multiple Foos, that'll be quite a refactor to implement it. Another (larger) downside is that we explicitly reference (i.e depend on) Foo, which isn't great and a problem if Bar is inside a project/ library that cannot directly reference Foo. Luckily solution 2 fixes those problems:
The more elegant solution: Events
public class Foo
{
// We don't need Foo to be a singleton anymore
public void FunctionToBeCalled() { /* Your code here */ }
}
public class Bar
{
public delegate void FunctionToBeCalledAfterEventHandler();
public event FunctionToBecalledAfterEventHandler FunctionToBeCalledAfterEvent;
public void FunctionToBeCalledAfter()
{
// Your existing code here
OnFunctionToBeCalledAfterEvent(); // Fire the event
}
private void OnFunctionToBeCalledAfterEvent()
{
FunctionToBeCalledEvent?.Invoke();
}
}
Now, everywhere where you're creating an instance of Bar you need to have a reference to Foo and subscribe to the event like so:
// foo = instance of class Foo
var bar = new Bar();
// The compiler is smart enough to find out that 'FunctionToBeCalledAfterEvent'
// has the same signature as 'FunctionToBeCalledAfterEvent' and can call it directly
// If this just so happens to not be case, see second way to subscribe to events
bar.FunctionToBeCalledAfterEvent += foo.FunctionToBeCalled;
// Or
bar.FunctionToBeCalledAfterEvent += () => foo.FunctionToBeCalled();
Events are great
Class B can have an event that other parties can handle. At the end of B.FunctionToBeCalledAfter this event would be invoked. Anyone who registered for this event would then be notified. Usual boilerplate code involves one virtual method that invokes one event. It's the standard way of adding events. If there is no need for additional data in the event then EventArgs is used. If additional data is needed then you could replace EventArgs with EventArgs<YourData>, or as an alternative, introduce a class XxxArgs derived from EventArgs with this additional data.
Class B
{
public event EventHandler FinishedFunctionToBeCalledAfter;
protected virtual void OnFinishedFunctionToBeCalledAfter(EventArgs e)
{
EventHandler handler = FinishedFunctionToBeCalledAfter;
handler?.Invoke(this, e);
}
public void FunctionToBeCalledAfter()
{
...
OnFinishedFunctionToBeCalledAfter(EventArgs.Empty);
}
}
Now when class A gets a hold of an object of class B it would add its event handler to it:
class A
{
public void FunctionToBeCalled();
public void FinishedFunctionToBeCalledAfter(object source, EventArgs e);
public void IntroduceObject(B b)
{
b.FinishedFunctionToBeCalledAfter += FinishedFunctionToBeCalledAfter;
}
}
When this object b of class B should end its life class A must know about it so that it can remove its event handler:
b.FinishedFunctionToBeCalledAfter -= FinishedFunctionToBeCalledAfter;
Class1 has event with attribute [EventPublication("event1")].
Class2 and Class3 inherits from Class1.
I want to subscribe Method1 to event in object from Class2 and Method2 to event in object from Class3 using [EventSubscription].
But in the derived classes there is the same EventPublication name of the event. So how to distinguish events in derived classes? Is it possible?
EDIT:
Maybe I misunderstand some obvious things about IoC or I try to complicate simple solution...
I will try to clarify my question. Here is some code:
class BasePresenter
{
[EventPublication("event")]
public event Action action;
public void Run()
{
someAction();
if (action != null)
action();
}
protected virtual void someAction()
{
}
}
class Presenter1 : BasePresenter
{
protected override void someAction()
{
}
}
class Presenter2 : BasePresenter
{
protected override void someAction()
{
}
}
class AnotherClass
{
[EventSubscription("event", ThreadOption.Caller)]
public void action1()
{
System.Windows.Forms.MessageBox.Show("Presenter1 started");
}
[EventSubscription("event", ThreadOption.Caller)]
public void action2()
{
System.Windows.Forms.MessageBox.Show("Presenter2 started");
}
}
There is action1() and action2() methods in Another class. I would like to fire action1() when instance of Presenter1 Run() method is called and fire action2() when instance of Presenter2 Run() method is called. But calling Run() method will fire both methods action1 and action2.
I'm not certain I understand the question. There are two ends to the event aggregation, a Publisher and a Subscriber. They are "connected" by the string event name you use in the attribute and nothing else.
A subscription can be done in the same class as the publication, though it's not clear to me why you'd ever do that, just have the base class call a virtual method that the derived classes implement and you're done.
If you want to use events and you want to know if the event source instance is not the receiver instance, just check the event's source input parameter against this, something along these lines:
[EventSubscription("myevent")]
public void OnEvent(object sender, EventArgs a)
{
if(sender.Equals(this)) return;
// do stuff here - the event came from another class instance
}
In one class I'm adding objects to my ObservableCollection. And in another class, I'm doing stuff with my added object and then delete it from the collection.
Those two classes cannot communicate with each other, so I decided to go for static collection (I only have access to the class definition for some reason)
In my first class, all elements are added properly (I checked the Count property), in the second class I subscribe to the CollectionChanged event. However, the event is not raising. I think it's because of the statickeyword, but I'm not sure.
Here is a code sample:
static public class A
{
public static ObservableCollection<object> MyCollection = new ObservableCollection<object>();
}
public class B
{
public B()
{
A.MyCollection.CollectionChanged += Func_CollectionChanged;
}
void Func_CollectionChanged(...)
{
//Stuff
}
}
public class C
{
public void func()
{
A.MyCollection.Add(object);
}
}
Here it works fine for me:
class Program
{
static void Main(string[] args)
{
B obj = new B();
}
}
public class A
{
public static ObservableCollection<object> MyCollection = new ObservableCollection<object>();
}
public class B
{
public B()
{
A.MyCollection.CollectionChanged += Func_CollectionChanged;
A.MyCollection.Add(1);
}
private void Func_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
// do some stuff here
}
}
by using A.MyCollection.CollectionChangedline you are creating an EventHandler to handle the the collection change event. it fires when ever any changes(add/update/delete) made in the collection. since it is a delegate you are creating you have to specify the sender who own the event and the type of arguments(What it going to handle), in-order to get proper reporting of published event
Updates
You just look into your code. the instance of class b is not yet created, the constructor of this class will automatically invoked only when the new instance of the class is created. You are creating the Event handler inside the constructor of class b. So it is not yet published any event. that is the reason for the collection_Change event is not triggering in your code snippet.
Hence your Definition for class C will be like the following to register the event :
public class C
{
B obj = new B();
public void func()
{
A.MyCollection.Add(1);
}
}
I've been looking for several days for the solution to a trivial problem in C or C++ but seemingly impossible in C#.
Another programmer creates class A. An event handler method in class A needs to call a method in my class B to process the event. Depending on the type of event, that method in class B needs to callback a method in class A, and that method has an arbitrary name or may not even exist.
This is simple in C; you just have a pointer to the callback function and call it indirectly if it isn't null. I don't know how to do indirect method calls in C#. Here's an example of some code illustrating the problem.
public class A: ZZZ { // this class is NOT under my control
private b = new B(this);
public void myCallback(C x) {
// do something
}
// Elsewhere in the application expects a protected
// override method to exist in *this* class A to handle
// an event. But we want a method in class B to handle
// it and then call myCallback depending on the type of event
protected override void handle_some_event(event e) {
// doesn't work -- how do I pass a "pointer" to the callback??
b.handle_event(e, myCallback);
}
}
public class B { // this class IS under my control
private A base;
public B(A a) {
base = a; // allows for calling methods in class A from class B
}
public handle_event(event e, ??? callback pointer ??? cback) {
// do stuff...
// then do the callback
// cback(); // this won't work
base.myCallback(); // this WILL work but only if I hard-code "myCallback"
}
}
The problem is that class B is the one I'm writing, and class A is authored by someone else who will be using my class B. That someone else has the option of not defining a callback at all, or creating one with any arbitrary name. Class B needs to know somehow what that is and how to access it. In C or C++ the other programmer could simply pass a pointer to his callback function. Is that possible in C#?
You want to pass an Action<T> to the handle_some_event you can do that like this:
public handle_some_event(event e, Action<C> cback)
{
// Do stuff
if(cback != null)
cback(myC);
}
Your class B in fact doesn't need to know anything at all about class A, it just needs an Action delegate to execute, the rest is irrelevant.
You can use Action delegate to refer to the callback.
Check this URL for more info: http://msdn.microsoft.com/en-us/library/018hxwa8.aspx
Change you class B to :
public class B {
private A base;
public B(A a) {
base = a; // allows for calling methods in class A from class B
}
//#################### NOTE THE CHANGE HERE ##################//
public handle_some_event(event e, Action<C> cback) {
// do stuff...
// then do the callback
cback(x); // this will work assuming you have x defined somewhere in B
}
}
Here is the solution I ended up with, based on the code in my initial post
public class A: ZZZ { // this class is NOT under my control
private b = new B(this);
b.UserCallback = myCallback;
static void myCallback(C x) {
// do something
}
// Elsewhere in the application expects a protected
// override method to exist in *this* class A to handle
// an event. But we want a method in class B to handle
// it and then call myCallback depending on the type of event
protected override void handle_some_event(event e) {
b.handle_event(e);
}
}
public delegate void usercallback(Z z); // declare OUTSIDE both classes
public class B { // this class IS under my control
public usercallback UserCallback = defaultCallback;
static void defaultCallback(Z z) { /* do nothing */ }
public handle_event(event e) {
// do stuff...
// then do the callback
UserCallback(z);
}
}
What got me stuck for a while is wrapping my head around the fact that a delegate declaration is analogous to a C function prototype, and that a delegate may be used as a declaration type in a class.
In following code, I want to extend the behaviour of a class by deriving/subclassing it, and make use of an event of the base class:
public class A
{
public event EventHandler SomeEvent;
public void someMethod()
{
if(SomeEvent != null) SomeEvent(this, someArgs);
}
}
public class B : A
{
public void someOtherMethod()
{
if(SomeEvent != null) SomeEvent(this, someArgs); // << why is this not possible?
//Error: The event 'SomeEvent' can only appear on the left hand side of += or -=
//(except when used from within the type 'A')
}
}
Why isn't it possible?
And what is the common solution for this kind of situation?
Others have explained how to get round the issue, but not why it's coming up.
When you declare a public field-like event, the compiler creates a public event, and a private field. Within the same class (or nested classes) you can get at the field directly, e.g. to invoke all the handlers. From other classes, you only see the event, which only allows subscription and unsubscription.
The standard practice here is to have a protected virtual method OnSomeEvent on your base class, then call that method in derived classes. Also, for threading reasons you will want to keep a reference to the handler before checking null and calling it.
For an explanation of the why read Jon Skeet's answer or the C# specification which describes how the compiler automatically creates a private field.
Here is one possible work around.
public class A
{
public event EventHandler SomeEvent;
public void someMethod()
{
OnSomeEvent();
}
protected void OnSomeEvent()
{
EventHandler handler = SomeEvent;
if(handler != null)
handler(this, someArgs);
}
}
public class B : A
{
public void someOtherMethod()
{
OnSomeEvent();
}
}
Edit: Updated code based upon Framework Design Guidelines section 5.4 and reminders by others.
Todd's answer is correct. Often you will see this implemented throughout the .NET framework as OnXXX(EventArgs) methods:
public class Foo
{
public event EventHandler Click;
protected virtual void OnClick(EventArgs e)
{
var click = Click;
if (click != null)
click(this, e);
}
}
I strongly encourage you to consider the EventArgs<T>/EventHandler<T> pattern before you find yourself making all manner of CustomEventArgs/CustomEventHandler for raising events.
The reason the original code doesn't work is because you need to have access to the event's delegate in order to raise it, and C# keeps this delegate private.
Events in C# are represented publicly by a pair of methods, add_SomeEvent and remove_SomeEvent, which is why you can subscribe to an event from outside the class, but not raise it.
My answer would be that you shouldn't have to do this.
C# nicely enforces Only the type declaring/publishing the event should fire/raise it.
If the base class trusted derivations to have the capability to raise its events, the creator would expose protected methods to do that. If they don't exist, its a good hint that you probably shouldn't do this.
My contrived example as to how different the world would be if derived types were allowed to raise events in their ancestors. Note: this is not valid C# code.. (yet..)
public class GoodVigilante
{
public event EventHandler LaunchMissiles;
public void Evaluate()
{
Action a = DetermineCourseOfAction(); // method that evaluates every possible
// non-violent solution before resorting to 'Unleashing the fury'
if (null != a)
{ a.Do(); }
else
{ if (null != LaunchMissiles) LaunchMissiles(this, EventArgs.Empty); }
}
virtual protected string WhatsTheTime()
{ return DateTime.Now.ToString(); }
....
}
public class TriggerHappy : GoodVigilante
{
protected override string WhatsTheTime()
{
if (null != LaunchMissiles) LaunchMissiles(this, EventArgs.Empty);
}
}
// client code
GoodVigilante a = new GoodVigilante();
a.LaunchMissiles += new EventHandler(FireAway);
GoodVigilante b = new TriggerHappy(); // rogue/imposter
b.LaunchMissiles += new EventHandler(FireAway);
private void FireAway(object sender, EventArgs e)
{
// nuke 'em
}
Wrap it with a protected virtual On... method:
public class BaseClass
{
public event EventHandler<MyArgs> SomeEvent;
protected virtual void OnSomeEvent()
{
if(SomeEvent!= null)
SomeEvent(this, new MyArgs(...) );
}
}
Then override this in a derived class
public class DerivedClass : BaseClass
{
protected override void OnSomeEvent()
{
//do something
base.OnSomeEvent();
}
}
You'll set this pattern all over .Net - all form and web controls follow it.
Do not use the prefix Raise... - this is not consistent with MS's standards and can cause confusion elsewhere.