3

Usually when people get a "No overload for 'onTimer' matches delegate 'ElapsedEventHandler'" error, it's due to not including the ElapsedEventArgs parameter in the void that runs every time the timer has elapsed. Also, the error seems to disappear when I copy and paste the code into a file that's outside of the solution, so I'm confused. Does anybody know a simpler way to fix this?

    static void setTimer()
    {
        yearIncreaser.Elapsed += onTimer;
        yearIncreaser.Enabled = true;
        yearIncreaser.AutoReset = true;
    }

    static void onTimer(Object source, ElapsedEventArgs e)
    {
        currentYear++;
    }

EDIT: Yes, the official error message that the debugging screen produces is: error CS0123: No overload for 'onTimer' matches delegate 'ElapsedEventHandler'

EDIT2: The solution was changing yearIncreaser.Elapsed += onTimer; into a lambda: yearIncreaser.Elapsed += (sender, args) => { }

Golden_Eagle
  • 124
  • 1
  • 9
  • 1
    Be clear about 'Warning' vs 'Error'. Best to post the literal error message. – H H Mar 15 '20 at 18:37
  • Since it's only happening in one project, try explicitly specifying the types. Initialize your `yearIncreaser` like this: `new System.Timers.Timer()` and your `ElapsedEventArgs` like this: `System.Timers.ElapsedEventArgs` – Chronicle Mar 15 '20 at 18:52
  • What do you mean with "it's due to __not__ including the ElapsedEventArgs parameter" ? – H H Mar 15 '20 at 18:52
  • 1
    Also try using a lambda in your `Elapsed` like this: `yearIncreaser.Elapsed += (sender, args) => { }`. Then in visual studio hover over `sender` and `args` to see if their types are what you expect – Chronicle Mar 15 '20 at 18:53
  • @Henk Holterman The only other time i saw that someone had the same problem using System.Timers.Timer was when they typed something like static void onTimer() instead of static void onTimer(Object source, ElapsedEventArgs e). I could only reproduce the error in another C# file by typing onTimer(Object source) (removing the "ElapsedEventArgs e" parameter). – Golden_Eagle Mar 15 '20 at 19:24

1 Answers1

2

Maybe someone else will need it.

I just changed Object to object:

    static void setTimer()
    {
        yearIncreaser.Elapsed += onTimer;
        yearIncreaser.Enabled = true;
        yearIncreaser.AutoReset = true;
    }

    static void onTimer(object source, ElapsedEventArgs e)
    {
        currentYear++;
    }
Arsen Tatraev
  • 470
  • 1
  • 4
  • 16