I have 3 classes A,B,C .
Class A creates B .. class B creates class C.
Class C raises events after some action/operation with some data, which is handled by event handler in Class B.
Now I want to be handle or pass the same raised event data to Class A.
I know i can raise another event from class B and handle it in A but is there a better
way of handling such events??
Edit :
not using Inheritance.i will give a psuedo-code ..
please ignore syntax as such.I have done something like this.
Class A
{
B objB;
public void init()
{
objB= new B();
addeventhandler(objB);
objB.init();
}
//Suppose handle response_objB_Event handles event raised by C
private void handleEvent_objB_Event(string message)
{
doSomething(message);
}
}
Class B
{
C objC;
public void init()
{
objC= new C();
addeventhandler(objC);
objC.DoOperation();
}
//Suppose response_objC_Event handles event raised by C
private void handleEvent_objC_Event(string message)
{
//doSomething(message);
again raise event to pass 'message' to B.
}
private void doSomething(string message)
{
//.......do something
}
}
Class C
{
Event evtOperationComplete;
public void DoOperation()
{
//.......do something
// after coompletion
OperationComplete();
}
public void OperationComplete()
{
RaiseEvent(evtOperationComplete,message);
}
public void RaiseEvent(Event E,String message)
{
// raise/invoke event/delgate here
}
}
oh ok thx 🙂 ..
Edit#2
Actually A is Form which creates B which is SocketManager and C is the actual socket class which sends and receives data..when C receives data/message , I want to display it on the Form i.e. A .so I raised events from C->B->A.
Thx
If you’re not editing the data in the event handler of Class B, then simply have both class A and B subscribe to the event in C. If you’re editing the data in B for A, then I’d recommend just calling a standard method in A with the data from B.
UPDATE:
Based on your clarifications, I’d say just have 2 events – your socket (C) class fires an event when it has data, your socket manager (B) listens for this event and fires another, separate event when it has this data. Your form (A) listens for this event from B and acts on it. That makes your intent clear enough.