Haim Kashi
Haim Kashi

Reputation: 399

Why im getting an error when creating an event in my new class?

I didn't understand the erorr message and what to do to fix it and why it happen. This is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Threading;

namespace GatherLinks
{
    class BackgroundWebCrawling
    {
        public string f;
        int counter = 0;
        List<string> WebSitesToCrawl;
        int MaxSimultaneousThreads;
        BackgroundWorker mainBackGroundWorker;
        BackgroundWorker secondryBackGroundWorker;
        WebcrawlerConfiguration webcrawlerCFG;
        List<WebCrawler> webcrawlers;
        int maxlevels;
        public event EventHandler<BackgroundWebCrawling> ProgressEvent;

The error is on the ProgressEvent

Error 1 The type 'GatherLinks.BackgroundWebCrawling' cannot be used as type parameter 'TEventArgs' in the generic type or method 'System.EventHandler'. There is no implicit reference conversion from 'GatherLinks.BackgroundWebCrawling' to 'System.EventArgs'.

Upvotes: 2

Views: 2791

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062780

The EventHandler<T> signature is (at least, originally) intended for scenarios where the args (in the common sender / args pattern) is some subclass of EventArgs. As such, there is a where T : EventArgs constraint (edit - as ByteBlast notes: until .NET 4.5, where the constraint is removed)

BackgroundWebCrawling is not an EventArgs. Besides which, there would be no point sending it as the args, as you are presumably already sending it (this) as the sender.

If you have no interesting args to send, just use the non-generic EventHandler, and send EventArgs.Empty.

Upvotes: 10

Related Questions