Anonymous Method syntax issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joel Finkel

    #1

    Anonymous Method syntax issue

    Folks,

    Sorry to take up bandwidth, but for the life of me I cannot see the syntax
    error in this code:

    Timer myTimer = new Timer();
    myTimer.Tick += delegate {
    stopWaiting = true;
    };

    The compiler error indicates that the parser is exepcting a "}" at the "."
    in the second line. This is the first error in the source file.

    By the way, the same error is thrown if I use either of these two variations:

    myTimer.Tick += delegate() {
    stopWaiting = true;
    };

    myTimer.Tick += delegate(object sender, EventArgs e) {
    stopWaiting = true;
    };

    Thanks in advance for your assistance.

    /Joel Finkel
    finkel@sd-il.com
  • Jon Skeet [C# MVP]

    #2
    Re: Anonymous Method syntax issue

    Joel Finkel <JoelFinkel@dis cussions.micros oft.com> wrote:[color=blue]
    > Sorry to take up bandwidth, but for the life of me I cannot see the syntax
    > error in this code:
    >
    > Timer myTimer = new Timer();
    > myTimer.Tick += delegate {
    > stopWaiting = true;
    > };
    >
    > The compiler error indicates that the parser is exepcting a "}" at the "."
    > in the second line. This is the first error in the source file.[/color]

    Are you absolutely sure you're using the C# 2.0 compiler? This code
    compiles fine on my box with 2.0, but gives the same kind of error
    you're talking about if I try to compile it with 1.1...

    using System;
    using System.Windows. Forms;

    public class Test
    {
    static Timer myTimer;

    static bool stopWaiting;

    static void Main()
    {
    myTimer = new Timer();

    myTimer.Tick += delegate(object sender, EventArgs e)
    {
    stopWaiting = true;
    };

    myTimer.Tick += delegate { stopWaiting = true; };
    }

    }

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • chris martin

      #3
      Re: Anonymous Method syntax issue

      > Folks,[color=blue]
      >
      > Sorry to take up bandwidth, but for the life of me I cannot see the
      > syntax error in this code:
      >
      > Timer myTimer = new Timer();
      > myTimer.Tick += delegate {
      > stopWaiting = true;
      > };[/color]

      Compiles over here.
      [color=blue]
      > The compiler error indicates that the parser is exepcting a "}" at the
      > "." in the second line. This is the first error in the source file.
      >
      > By the way, the same error is thrown if I use either of these two
      > variations:
      >
      > myTimer.Tick += delegate() {
      > stopWaiting = true;
      > };[/color]

      Won't compile.
      [color=blue]
      > myTimer.Tick += delegate(object sender, EventArgs e) {
      > stopWaiting = true;
      > };
      > Thanks in advance for your assistance.
      >[/color]

      Compiles fine.
      [color=blue]
      > /Joel Finkel
      > finkel@sd-il.com[/color]

      I can't get the event to fire though.


      Comment

      • Joel Finkel

        #4
        Re: Anonymous Method syntax issue

        I have not a clue what the problem is. I brought the code into an existing
        project and it compiles fine. Somehow, there must have been some crud in the
        editor.

        Hmmm...I guess that means I DO have a clue what the problem is; but I'm not
        going to pursue it further.

        Thanks.

        "chris martin" wrote:
        [color=blue][color=green]
        > > Folks,
        > >
        > > Sorry to take up bandwidth, but for the life of me I cannot see the
        > > syntax error in this code:
        > >
        > > Timer myTimer = new Timer();
        > > myTimer.Tick += delegate {
        > > stopWaiting = true;
        > > };[/color]
        >
        > Compiles over here.
        >[color=green]
        > > The compiler error indicates that the parser is exepcting a "}" at the
        > > "." in the second line. This is the first error in the source file.
        > >
        > > By the way, the same error is thrown if I use either of these two
        > > variations:
        > >
        > > myTimer.Tick += delegate() {
        > > stopWaiting = true;
        > > };[/color]
        >
        > Won't compile.
        >[color=green]
        > > myTimer.Tick += delegate(object sender, EventArgs e) {
        > > stopWaiting = true;
        > > };
        > > Thanks in advance for your assistance.
        > >[/color]
        >
        > Compiles fine.
        >[color=green]
        > > /Joel Finkel
        > > finkel@sd-il.com[/color]
        >
        > I can't get the event to fire though.
        >
        >
        >[/color]

        Comment

        Working...