Click here to Skip to main content
15,895,283 members
Home / Discussions / C#
   

C#

 
GeneralRe: about Visual studio 2010 beta 1 Pin
Christian Graus30-Jun-09 13:28
protectorChristian Graus30-Jun-09 13:28 
GeneralRe: about Visual studio 2010 beta 1 Pin
Seraph_summer30-Jun-09 23:35
Seraph_summer30-Jun-09 23:35 
GeneralRe: about Visual studio 2010 beta 1 Pin
Dan Neely1-Jul-09 10:59
Dan Neely1-Jul-09 10:59 
Questioncurve from bezier cusps Pin
googlejumbo30-Jun-09 10:11
googlejumbo30-Jun-09 10:11 
AnswerRe: curve from bezier cusps Pin
molesworth30-Jun-09 22:09
molesworth30-Jun-09 22:09 
GeneralRe: curve from bezier cusps Pin
googlejumbo1-Jul-09 7:12
googlejumbo1-Jul-09 7:12 
GeneralRe: curve from bezier cusps Pin
molesworth1-Jul-09 11:42
molesworth1-Jul-09 11:42 
QuestionAnonymous methods problem Pin
Narvius30-Jun-09 10:07
Narvius30-Jun-09 10:07 
AnswerRe: Anonymous methods problem Pin
Christian Graus30-Jun-09 10:29
protectorChristian Graus30-Jun-09 10:29 
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 

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.