开发者

C# Event handling In mutiple level /classes

开发者 https://www.devze.com 2022-12-28 06:13 出处:网络
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.

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,S开发者_如何学JAVAtring 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


Based on my previous answer, it sounds like you have an inheritance heirarchy:

public class A
public class B : A
public class C : B

in which case you shouldn't use events for this. Rather, use base to call each base class.

class Program
{
    static void Main(string[] args)
    {
        C c = new C();
        c.DoStuff(0);
    }
}

public class A
{
    public virtual void DoStuff(int x)
    {
        Console.WriteLine(x);
    }
}

public class B : A
{
    public override void DoStuff(int y)
    {
        y++;
        base.DoStuff(y);
    }
}

public class C : B
{
    public override void DoStuff(int z)
    {
        z++;
        base.DoStuff(z);
    }
}

This example shows creation of your class C and its subsequent editing of data. This data is then passed on to B, which edits the data again before passing onto A.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号