Calling Classes in MainWindow from MDI Child Window

  • Thread starter Thread starter Shaun Pudwell
  • Start date Start date
S

Shaun Pudwell

Hi, I'm new to c# and come from a C / C++ background.
My MainWindow class has a function called DrawChart.
I open an MDI Child Window and want it to make a call from
its class MDIChild to DrawChart which is the MainWindow
class. Also, I want the chart counter to be incremented
on each call to DrawChart, without being set back to its
original value.

From my MDIChild class, If I say:

MainWindow xxx = new MainWindow ();
xxx.DrawChart ();

My counter gets re-initialized every time I call DrawChart.
Can anyone provide an answer ( in code ) if possible.

Thanks in advance.
Shaun Pudwell.
 
Hi Shaun,

Shaun Pudwell said:
Hi, I'm new to c# and come from a C / C++ background.
My MainWindow class has a function called DrawChart.
I open an MDI Child Window and want it to make a call from
its class MDIChild to DrawChart which is the MainWindow
class. Also, I want the chart counter to be incremented
on each call to DrawChart, without being set back to its
original value.

From my MDIChild class, If I say:

MainWindow xxx = new MainWindow ();
xxx.DrawChart ();

My counter gets re-initialized every time I call DrawChart.
Can anyone provide an answer ( in code ) if possible.

Hmm...The problem is apparent, but the explanation is not. You are
creating a new instance of MainWindow each time and calling DrawChart on
that. I assume that you *want* to call the DrawChart method on the instance
of the MainWindow class that encapsulates the visible MDI parent form (which
is, in turn, parent to the MDI child windows). This can most easily be done
like this:

// Within an MDI child window class.
MainWindow xxx = (MainWindow)this.MdiParent;
xxx.DrawChart ();

Regards,
Dan
 
Hi,

Thanks, I will try that one.

Didn't see anything like this in either of the two books I have nor in
the Scribble demo!


Regards

Shaun Pudwell.
 
Shaun,

While there is certainly nothing wrong with this approach, you will
now have a circular reference. The MDIParent object has a reference
to the MDIChild object and the MDIChild object has a reference to the
parent. You have to be Very careful with circular references.

Another approach would be to put an event in the MDIChild class. In
the MDIParent class write an event handler that calls or does your
drawing task. Then in the child you simply raise the event whenever
you want the drawing to happen.

This is just another approach FYI.
 
Hi Doug,

Doug Taylor said:
Shaun,

While there is certainly nothing wrong with this approach, you will
now have a circular reference. The MDIParent object has a reference
to the MDIChild object and the MDIChild object has a reference to the
parent. You have to be Very careful with circular references.

Another approach would be to put an event in the MDIChild class. In
the MDIParent class write an event handler that calls or does your
drawing task. Then in the child you simply raise the event whenever
you want the drawing to happen.

This is just another approach FYI.

A few things:

1. What I suggested to Shaun will not create a circular reference
situation. In the code snippet, the MainWindow variable is declared local to
the routine. Once the routine exits, the variable goes out of scope and the
reference is no more.

2. If Shaun *were* to create a circular reference situation (say he
assigns a reference to the MainWindow instance to a *member* variable of the
child window instance) this would still be no big deal. The garbage
collector implementation in .NET will still reclaim both instances when they
are no longer "reachable" by the application.

3. Subscribing to an event creates a reference from the event source
object to the event subscriber object. Thus your suggestion would, in fact,
create a circular reference situation. Fortunately this is no big deal (see
#2).

Regards,
Dan
 
Daniel,

All very good points. Thanks.

Daniel Pratt said:
Hi Doug,



A few things:

1. What I suggested to Shaun will not create a circular reference
situation. In the code snippet, the MainWindow variable is declared local to
the routine. Once the routine exits, the variable goes out of scope and the
reference is no more.

2. If Shaun *were* to create a circular reference situation (say he
assigns a reference to the MainWindow instance to a *member* variable of the
child window instance) this would still be no big deal. The garbage
collector implementation in .NET will still reclaim both instances when they
are no longer "reachable" by the application.

3. Subscribing to an event creates a reference from the event source
object to the event subscriber object. Thus your suggestion would, in fact,
create a circular reference situation. Fortunately this is no big deal (see
#2).

Regards,
Dan
 
Back
Top