3

I am not sure what is wrong with my code can someone help fix the error? The error is in the timer.Tick() line. It's supposed to make a stopwatch.

namespace App3
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {                
            this.InitializeComponent();
        }
        private int myCount;

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Tick += new EventHandler<object>(timer_Tick);
            timer.Interval = TimeSpan.FromSeconds(5);
            timer.Start();    
        }


        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            myCount++;
            Label.Text = myCount.ToString();
        }
    }
Brian
  • 5,069
  • 7
  • 37
  • 47
bluemagic
  • 29
  • 1
  • 6
  • possible duplicate of [No overload for 'method' matches delegate 'System.EventHandler'](http://stackoverflow.com/questions/8067246/no-overload-for-method-matches-delegate-system-eventhandler) – mbeckish Feb 25 '13 at 20:45
  • @mbeckish Similar name to this question, but the actual problem in that question is different. – Reed Copsey Feb 25 '13 at 20:47
  • @ReedCopsey - But wouldn't you agree that the underlying misunderstanding, and the solution, are also the same? DispatcherTimer.Tick is an EventHandler, and the signature of the method chosen to handle the event must match? – mbeckish Feb 25 '13 at 21:11
  • @mbeckish It's a similar problem, but there the event subscription was perfectly fine - it's the method that's incorrect. Here the opposite is true. Granted, they're the same underlying issue of delegate/method syntax mismatches, but the details are completely different, so I would understand why a user would see these as different issues, and I wouldn't think of them as "duplicate" questions. – Reed Copsey Feb 25 '13 at 21:17
  • @ReedCopsey - On second thought, I agree. Too bad I can't unvote to close. – mbeckish Feb 25 '13 at 21:18

4 Answers4

5

DispatcherTimer.Tick is an EventHandler, not an EventHandler<object>.

You need to change your code to specify this correctly:

 timer.Tick += new EventHandler(timer_Tick);

Note that this can also be written in short form, which is typically safer:

timer.Tick += timer_Tick;
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
4

Delegate types are not required when attaching events, the compiler can figure it out for you. Try this instead:

timer.Tick += timer_Tick;

As for why you get the error currently, the EventHandler delegate is declared like this:

public delegate void EventHandler<TEventArgs>(
    Object sender,
    TEventArgs e
)

This means the type you put in the brackets represents the second argument in the function.

So when you type EventHandler<object>, the compiler looks for a function like timer_Tick(Object sender, object e) which it does not find, hence the error. To use the full delegate type, it would have to be EventHandler<EventArgs>

WildCrustacean
  • 5,896
  • 2
  • 31
  • 42
1

Instead of new EventHandler<object>, do new EventHandler. Or alternatively just put += timer_Tick;.

It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
1

I solved the same issue with two small changes:

1) Change second argument to type object:

INSTEAD OF THIS:

private void timer_Tick(object sender, EventArgs e)

USE THIS:

private void timer_Tick(object sender, **object** e)

2) Simplify the

INSTEAD OF THIS:

timer.Tick += new EventHandler<object>(timer_Tick);

USE THIS:

timer.Tick += timer_Tick;  // as suggested by WildCrustacean above
flx
  • 14,146
  • 11
  • 55
  • 70
Andrew
  • 11
  • 1