views:

44

answers:

2

I have a class A, B

    class A
    {
        public class B
        {
            public event EventHandler handleClick;
            public void eventraising(object sender, EventArgs e)
            {
                if (handleClick != null)
                    handleClick(this, e);
            }
        }

        //handle raised event here
    }

How can I do this

A: 

You need an instance of B in A.

class A
{
    B b = new B;

    A()
    {
        b.handleClick += (sender, ev) => { ... }
    }
}
Gary
+2  A: 

Class A would need an instance of B somewhere first. Nesting classes in .NET is simply a way of organizing the types. It doesn't imply any kind of ownership of an instance.

So you'd have to do something like:

class A
{

    private B instanceOfB;

    // constructor
    public A() {
        instanceOfB = new B();
        instanceOfB.handleClick += instanceOfB_handleClick;
    }

    // event handler
    private void instanceOfB_handleClick(object sender, EventArgs e) {
        // blah
    }

    public class B
    {
        public event EventHandler handleClick;
        public void eventraising(object sender, EventArgs e)
        {
            if (handleClick != null)
                handleClick(this, e);
        }
    }

}

Also, your event should just be called Click, not handleClick.

Josh Einstein