Why does this work

  • Thread starter Thread starter Tony
  • Start date Start date
T

Tony

Most of the time I'm asking why something doesn't work but this time I'm
baffled as why this DOES work....

Note that the local variables wBatchUC, wModalPromptUC, and wBatchFlowPanel
are all stack variables and after the method executes, the stack rolls back,
but when the event handler gets called, the stack variables still retain the
original values - I have no idea why or how this works but the code works
flawlessly.


internal void DeleteBatch(BatchUC wBatchUC)
{
this.InModalMode = true;
ModalPromptUC wModalPromptUC = new ModalPromptUC(this, "You are
about to delete this batch, are you sure?");
wModalPromptUC.Initialize(m_oIAActiveUISnapin);
wModalPromptUC.Visible = false;

BatchFlowPanel wBatchFlowPanel = FindBatchFlowPanel(wBatchUC);

wBatchFlowPanel.Controls.Add(wModalPromptUC);
wBatchFlowPanel.Controls.SetChildIndex(wModalPromptUC,
wBatchFlowPanel.Controls.GetChildIndex(wBatchUC) + 1);
AnimateHideShow(null, wModalPromptUC);

wModalPromptUC.Reply += delegate(DialogResult eDialogResult)
{
AnimateHideShow(wModalPromptUC, null);
this.InModalMode = false;

if (eDialogResult == DialogResult.Yes)
{
wBatchUC.IABatch.Delete();

if (m_wSelectedwBatchUC == wBatchUC)
m_wSelectedwBatchUC = null;

m_wBatchFlowPanels.Controls.Remove(wBatchFlowPanel);
wBatchUC.Dispose();
}
else
wBatchFlowPanel.Controls.Remove(wModalPromptUC);

wModalPromptUC.Dispose();
this.InModalMode = false;
};
}
 
Tony said:
Most of the time I'm asking why something doesn't work but this time I'm
baffled as why this DOES work....

Note that the local variables wBatchUC, wModalPromptUC, and wBatchFlowPanel
are all stack variables and after the method executes, the stack rolls back,
but when the event handler gets called, the stack variables still retain the
original values - I have no idea why or how this works but the code works
flawlessly.

The variables aren't actually on the stack any more. They're *captured*
by the anonymous method.

See http://pobox.com/~skeet/csharp/csharp2/delegates.html
for more information.
 
Tony,

It's because of the nature of anonymous delegates. The compiler
actually generates code that makes sure that the stack variables that you
use are available to the anonymous delegate.

If you take a look in reflector, the compiler creates a type which has
the code in the method you are executing. It also has fields on the type
which corresponds to the values on the stack that you are using. Then, at
the call site, you will see an instance of this class is created and the
fields assigned when you work with the stack variables.
 
Hi,

Tony said:
Most of the time I'm asking why something doesn't work but this time I'm
baffled as why this DOES work....
:)


wModalPromptUC.Reply += delegate(DialogResult eDialogResult)
{
AnimateHideShow(wModalPromptUC, null);
this.InModalMode = false;

if (eDialogResult == DialogResult.Yes)
{
wBatchUC.IABatch.Delete();

if (m_wSelectedwBatchUC == wBatchUC)
m_wSelectedwBatchUC = null;

m_wBatchFlowPanels.Controls.Remove(wBatchFlowPanel);
wBatchUC.Dispose();
}
else
wBatchFlowPanel.Controls.Remove(wModalPromptUC);

wModalPromptUC.Dispose();
this.InModalMode = false;
};
}

Here is the answer to your question, the delegate will exist outside the
method you declared it in. (it will exist as long as the wModalPromptUC is
alive) so what would happen when it gets called outside your method if the
variable it refers are not longer alive?

What happens here is that the compiler rewrite the code and most probably
create a renamed variable as a member of the class and use it inside the
delegate.

I bet that if you check the docs of the Delegate you will find the
explanation.
 
<"Ignacio Machin \( .NET/ C# MVP \)" <machin TA laceupsolutions.com>>
wrote:

I bet that if you check the docs of the Delegate you will find the
explanation.

Nope - the docs for Delegate would be to do with the framework side of
things. Anonymous methods are no different to any other delegates as
far as the framework and CLR are concerned - they're a C# language
feature.

The C# language specification is the place to look for details.
 

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

Back
Top