Tal
Tal

Reputation: 355

Call a method from another method

I have this code:

        public void StartTimer()
        {
            var timer = new DispatcherTimer();
            timer.Tick += Something;
            timer.Interval = new TimeSpan(0, 0, 0, 3);
            timer.Start();

        }

        private async void Something()
        {      
         //code goes here....
        }

The problem is that I get this error:

No overload for 'Something' matches delegate 'System.EventHandler'

My question is probably basic: why I get this error and how can I fix it...

Upvotes: 0

Views: 64

Answers (4)

Khurram Sharif
Khurram Sharif

Reputation: 504

Try This

        public void StartTimer()
        {
            var timer = new DispatcherTimer();
            timer.Tick += Something;
            timer.Interval = new TimeSpan(0, 0, 0, 3);
            timer.Start();

        }

        private async void Something(Object sender, EventArgs e)
        {      
         //code goes here....
        }

Upvotes: 0

dcastro
dcastro

Reputation: 68660

The property Tick is of type EventHandler.

The signature of EventHandler is:

public delegate void EventHandler(
    Object sender,
    EventArgs e
)

Which means your Something method must match this signature. Change it to:

public void Something(Object sender, EventArgs e) { ... }

Alternatively, if you can't change the signature of that method, you can create a new delegate that calls your method.

timer.Tick += (s, e) => Something();

Upvotes: 3

JLRishe
JLRishe

Reputation: 101662

The DispatcherTimer.Tick event expects a handler that takes an object and an EventArgs argument:

private void Something(object o, EventArgs e)
{
    // implementation
}

Upvotes: 1

usr
usr

Reputation: 171178

The signature of Something does not match that specific event. Look into the documentation for that event to see how it's done.

Upvotes: 0

Related Questions