Click here to Skip to main content
15,867,568 members
Home / Discussions / C#
   

C#

 
GeneralRe: Anonymous methods problem Pin
Narvius30-Jun-09 10:37
Narvius30-Jun-09 10:37 
GeneralRe: Anonymous methods problem Pin
Christian Graus30-Jun-09 13:29
protectorChristian Graus30-Jun-09 13:29 
GeneralRe: Anonymous methods problem Pin
Narvius30-Jun-09 13:48
Narvius30-Jun-09 13:48 
GeneralRe: Anonymous methods problem Pin
dojohansen1-Jul-09 0:27
dojohansen1-Jul-09 0:27 
GeneralRe: Anonymous methods problem Pin
Narvius1-Jul-09 4:20
Narvius1-Jul-09 4:20 
GeneralRe: Anonymous methods problem Pin
dojohansen1-Jul-09 4:50
dojohansen1-Jul-09 4:50 
GeneralRe: Anonymous methods problem [modified] Pin
Narvius1-Jul-09 6:32
Narvius1-Jul-09 6:32 
GeneralRe: Anonymous methods problem Pin
dojohansen1-Jul-09 21:05
dojohansen1-Jul-09 21:05 
Thanks for that! But here too the delegate executes before the containing function returns. It's an EventHandler delegate, but it's used as any other delegate - there's no event.

Using an anonymous method for an event handler isn't necessarily weird, but accessing variables that are scoped to the containing method from the event handler is very weird indeed. I did a quick test to see how this behaved with a windows forms app. In the constructor I go into a loop to make 5 buttons, and I attach an anonymous method to the Click event. In the handler I show a messagebox displaying the value of the loop variable. I figured if the method was supposed to somehow "see" the external variable when I press a button, which I think we can agree won't happen until after the constructor has finished, then it would mean that five anonymous methods and five copies of this external variable had to be created. What actually happens though is that clicking any button shows the value 5, so presumably there is only one copy of the method.

What I am now wondering is where the heck this external variable lives. Normally local variables live on the stack, but if this one did it would have died when the constructor returned. So I presume it's on the heap somewhere. I'm not sure I like this.

My test code:

public Form1()
{
    InitializeComponent();
    for (int i = 0; i < 5; i++)
    {
        Button b = new Button() { Text = "button" + i, Top = 30*(i+1), Left = 50 };
        Controls.Add(b);
        b.Click += delegate(object sender, EventArgs e)
        {
            MessageBox.Show("variable i (scoped to Form1 constructor!) = " + i);
        };
    }
}

GeneralRe: Anonymous methods problem Pin
Narvius2-Jul-09 0:06
Narvius2-Jul-09 0:06 
QuestionCopy project files to a new project Pin
Member 354381030-Jun-09 9:17
Member 354381030-Jun-09 9:17 
AnswerRe: Copy project files to a new project Pin
Narvius30-Jun-09 10:18
Narvius30-Jun-09 10:18 
GeneralRe: Copy project files to a new project Pin
dojohansen1-Jul-09 0:36
dojohansen1-Jul-09 0:36 
QuestionUninstalling (setup and d eployment project) - newbie question Pin
choo_chu30-Jun-09 9:02
choo_chu30-Jun-09 9:02 
AnswerRe: Uninstalling (setup and d eployment project) - newbie question Pin
EliottA30-Jun-09 9:19
EliottA30-Jun-09 9:19 
GeneralRe: Uninstalling (setup and d eployment project) - newbie question Pin
choo_chu30-Jun-09 9:52
choo_chu30-Jun-09 9:52 
AnswerRe: Uninstalling (setup and d eployment project) - newbie question Pin
Jimmanuel30-Jun-09 10:08
Jimmanuel30-Jun-09 10:08 
GeneralRe: Uninstalling (setup and d eployment project) - newbie question Pin
choo_chu30-Jun-09 10:45
choo_chu30-Jun-09 10:45 
GeneralRe: Uninstalling (setup and d eployment project) - newbie question Pin
choo_chu30-Jun-09 11:22
choo_chu30-Jun-09 11:22 
GeneralRe: Uninstalling (setup and d eployment project) - newbie question Pin
Jimmanuel30-Jun-09 11:27
Jimmanuel30-Jun-09 11:27 
Questionoverriding System.Diagnostics..::.DebuggerStepThroughAttribute Pin
Dan Neely30-Jun-09 8:45
Dan Neely30-Jun-09 8:45 
AnswerRe: overriding System.Diagnostics..::.DebuggerStepThroughAttribute Pin
Rob Smiley30-Jun-09 11:43
Rob Smiley30-Jun-09 11:43 
GeneralRe: overriding System.Diagnostics..::.DebuggerStepThroughAttribute Pin
Dan Neely1-Jul-09 2:13
Dan Neely1-Jul-09 2:13 
Questionserial port communication between computer and another hardware device via RS232 in C# for windows Application Pin
Arpita Patel30-Jun-09 6:52
Arpita Patel30-Jun-09 6:52 
AnswerRe: serial port communication between computer and another hardware device via RS232 in C# for windows Application Pin
monstale30-Jun-09 7:08
monstale30-Jun-09 7:08 
GeneralRe: serial port communication between computer and another hardware device via RS232 in C# for windows Application Pin
Arpita Patel30-Jun-09 9:31
Arpita Patel30-Jun-09 9:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.