473,395 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Calling an event from an inner class

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
Mar 21 '06 #1
4 3152

"amaca" <pu****@ajam.net> wrote in message
news:up*************@TK2MSFTNGP10.phx.gbl...
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; }
}
Mar 21 '06 #2
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
Mar 21 '06 #3
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);
}
}

"amaca" <pu****@ajam.net> wrote in message
news:Oy**************@TK2MSFTNGP11.phx.gbl...
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

Mar 21 '06 #4
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
Mar 21 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Murat Tasan | last post by:
hi, i am having a small problem with constructing an inner class. i am using an inner class (and not a static nested class) because the methods of the inner class need access to the enclosing...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
3
by: NWx | last post by:
Hi, I defined a base class, Panel, based on System.Web.UI.UserControl, as below: Public MustInherit Class Panel Inherits System.Web.UI.UserControl ..... In another module, I want to...
12
by: Ron | last post by:
Greetings, I am trying to understand the rational for Raising Events instead of just calling a sub. Could someone explain the difference between the following 2 scenarios? Why would I want to...
5
by: Martijn Mulder | last post by:
A construction like this: class Outer { class Inner:Outer { } } compiles without problem but does it introduce infinity?
1
by: Memphis Steve | last post by:
Is it possible to combine multiple javascipts into one file and then call that file from a linked URL in the head section of an XHTML file? Here are the two scripts I want to use with the...
4
by: MLH | last post by:
I have the following saved UNION query named qryPeople2NameInNPaperAd: SELECT & " " & & " " & & " " & & ", " & & " " & AS Item, tblVehicleJobs.VehicleJobID FROM tblVehicleJobs INNER...
2
by: Kirk Strauser | last post by:
I'm trying to write a decorator that would do something like: def trace(before, after): def middle(func): def inner(*args, **kwargs): func.im_self.debugfunction(before) result = func(*args,...
9
by: Pubs | last post by:
Hi all, I want to call a function with some intial parameters with in a thread. At the end of the function execution it should return a value to the caller. Caller is outside the thread. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.