简体   繁体   中英

Events and Delegates in c# : How to use Events & Delegates to Notify One class about changes in other class

The question is below the proper way to use Events & Delegates to call method of class A:Show1() using event of class B which is raised in B:Show()

public class classA
{
    private classB _Thrower;

    public classA()
    {
        _Thrower = new classB();            
        _Thrower.ThrowEvent += (sender, args) => { show1(); };
    }

    public void show1()
    {
        Console.WriteLine("I am class 2");
    }
}

public class classB
{
    public delegate void EventHandler(object sender, EventArgs args);

    public event EventHandler ThrowEvent = delegate { };
    public classB()
    {
        this.ThrowEvent(this, new EventArgs());
    }
    public void show()
    {
        Console.WriteLine("I am class 1");            
    }
}

Sorry for any mistakes in question this is my first time of stackoverflow

Cant think of what you are trying to accomplish, but to answer your question this might do it. Unless I did not understand your question right.
Mind you that this violates a principle of OOP that states that each class should be independant of other classes.

public class classA
{
    public void show1()
    {
        Console.WriteLine("I am class A");
    }
}

public class classB
{
    public void show()
    {
        new classA().show1(); 
        Console.WriteLine("I am class B");            
    }
}

To get the result from your edited question you can use this code:

static void Main()
{
    classA obA = new classA();
    classB obB = new classB();
    obA.Shown += ( object sender, EventArgs e ) => { obB.ShowB(); };
    obA.ShowA();
}

public class classA
{
    public event EventHandler Shown;

    public classA()
    {
    }

    public void ShowA()
    {
        Console.WriteLine( "I am class A" );

        if( Shown != null )
            Shown( this, EventArgs.Empty );
    }
}

public class classB
{
    public event EventHandler Shown;

    public classB()
    {
    }

    public void ShowB()
    {
        Console.WriteLine( "I am class B" );

        if( Shown != null )
            Shown( this, EventArgs.Empty );
    }
}

In main() method you could do it vica versa as well:

obB.Shown += ( object sender, EventArgs e ) => { obA.ShowA(); };
obB.ShowB();

But you should not do both at the same time or you'll get an infinite loop ending up in a stackoverflow

I guess This is the answer @GuidoG @Kai Thoma

class A
{
    public A(B b)
    {
        b.ShowA += new methcall(this.showA);
    }
    public void showA()
    {
        Console.WriteLine("I am Class A");
    }
}
class B
{
    public event methcall ShowA;
    public void showB()
    {
        Console.WriteLine("I am Class B");
        if (ShowA != null)
            ShowA();
    }
}
class Program
{
    static void Main()
    {
        B b = new B();
        A a = new A(b);
        methcall m = new methcall(b.showB);
        m.Invoke();
        Console.ReadKey();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM