Jaydeep More Jaydeep More - 28 days ago 6
C# Question

How to can I use events and delegates to call methods from other classes without having object

How Can I use events and delegates to call methods of class A without using any reference to class A in class B

Answer

I guess You are looking For something like this

Hope This Helps

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();
    }
}
Comments