Calling an event from an inner class

A

amaca

In this slightly contrived (though small, complete and perfectly formed)
example:

public class Inner
{
public event EventHandler InnerHandler;

public void DoSomething()
{
if ( InnerHandler != null )
InnerHandler( this, EventArgs.Empty );
}

public Inner()
{
}
}

public class Outer
{
private Inner _Inner = new Inner();
public event EventHandler OuterHandler;

public Outer()
{
_Inner.InnerHandler += new EventHandler( _Inner_InnerHandler );
}

private void _Inner_InnerHandler( object sender, EventArgs e )
{
if ( OuterHandler != null )
OuterHandler( this, EventArgs.Empty );
}

public void Test()
{
_Inner.DoSomething();
}

}

calling Outer.Test() calls the inner eventhandler which calls the outer
eventhandler. Is there any way to do this without the intermediate step - to
set up InnerHandler to call a delegate in my main form without having to
bubble it out through a chain of event handlers?

Thanks,

Andrew
 
N

Nick Hounsome

amaca said:
In this slightly contrived (though small, complete and perfectly formed)
example:

public class Inner
{
public event EventHandler InnerHandler;

public void DoSomething()
{
if ( InnerHandler != null )
InnerHandler( this, EventArgs.Empty );
}

public Inner()
{
}
}

public class Outer
{
private Inner _Inner = new Inner();
public event EventHandler OuterHandler;

public Outer()
{
_Inner.InnerHandler += new EventHandler( _Inner_InnerHandler );
}

private void _Inner_InnerHandler( object sender, EventArgs e )
{
if ( OuterHandler != null )
OuterHandler( this, EventArgs.Empty );
}

public void Test()
{
_Inner.DoSomething();
}

}

calling Outer.Test() calls the inner eventhandler which calls the outer
eventhandler. Is there any way to do this without the intermediate step -
to set up InnerHandler to call a delegate in my main form without having
to bubble it out through a chain of event handlers?

Thanks,

Andrew

No - If you look at your code you will see why - the sender parameter of the
OuterHandler event is an Outer object and the Inner instance does not even
know the class Outer exists let alone the instance containing it.

If it wasn't for this logical design issue you could simply pass the
delegate through:

public event EventHandler OuterHandler
{
add { _Inner.InnerHandler += value; }
remove { _Inner.InnerHandler -= value; }
}
 
A

amaca

Thank you Nick - it was the syntax of add and remove I was after, and the
logical design issues can be worked round (unusually!)

Andrew
 
S

Stoitcho Goutsev \(100\)

amca,

If you declare your inner class inside the outer class declaration

class Outer
{
class Inner
{
.......
}
.......
}

then objects of Inner class will have acces to private and protected methods
of the outer class. What you need to do is when creating the Inner class
object to pass a reference to the outer class object then the inner class my
call

outer.RaiseOuterEvent() and it will fire the event of the outer class
without going thtough this intermediate-event step.

The follwoing is your code modified and not so nice formed

public class Outer
{

public class Inner
{
private Outer _Owner;
public Inner(Outer owner)
{
_Owner = owner;
}
public void DoSomething()
{
_Owner.OnOuterHandler(EventArgs.Empty);
}

}


private Inner _Inner;
public event EventHandler OuterHandler;

public Outer()
{
_Inner = new Inner(this);
}

public void Test()
{
_Inner.DoSomething();
}


protected virtual void OnOuterHandler(EventArgs e)
{
if (OuterHandler != null)
OuterHandler(this, e);
}
}
 
A

amaca

Thanks, Stoitcho, that's very helpful - I hadn't realized that the inner
class had access to the private methods of the outer class. (I have a slight
aversion to nested classes as in this case Inner and Outer are both going to
be large which gives me problems finding my around them, but I see I can use
partial classes to split them up into separate files).

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top