smriti
smriti

Reputation: 445

problem in delegate declaration and its invocation

When I try to call the event myevent in the function fire, it gives compiler error as

delegate mydelegate does not take 0 arguments.

if i try to give arguments in calling myevent as

myevent(this);

it again shows error as it does not take 1 parameters. what parameters am i supposed to give in the calling myevent? Here's the program code:

namespace useofdelegates
{
    public class Class1
    {   int i;
        public delegate void mydelegate(object sender,EventArgs e);
        public event mydelegate myevent;
        public void fire()
        {
            myevent(this); // *** shows compiler error ***
        }
    }
}

Upvotes: 2

Views: 136

Answers (4)

cecilphillip
cecilphillip

Reputation: 11596

You need to pass in 2 parameters. the first one being the source of the event. In your example "this" would work. The second one being of type EventArgs or a realted subclass. You can also pass EventArgs.Empty is you do not wish to pass any event especific information to the event handler.

So your method would look like this

fire(this, EventArgs.Empty);

or

fire(this, new MyCustomEVentArgs());

Here is an MSDN link with an example of using events http://msdn.microsoft.com/en-us/library/aa645739(VS.71).aspx

and nother one on creating custom EventArg subclasses to use in your handler. http://www.devarticles.com/c/a/C-Sharp/Creating-Custom-Delegates-and-Events-in-C-sharp/

Upvotes: 1

tster
tster

Reputation: 18257

Just create an empty EventArgs I guess.

myevent(this, new EventArgs());

Note, here is the full code:

namespace useofdelegates
{
    public class Class1
    {
        int i;
        public delegate void mydelegate(object sender, EventArgs e);
        public event mydelegate myevent;
        public void fire()
        {
            myevent(this, new EventArgs()); // *** shows compiler error ***
        }
    }
}

Upvotes: 0

pdr
pdr

Reputation: 6440

The compiler error is because you are using a delegate with two parameters and calling it with one.

The simple solution is

myevent(this, EventArgs.Empty);

The more regular (and thread-safe) solution, which I think you're getting confused with is

OnMyEvent(EventArgs.Empty);

// and then later

public void OnMyEvent(EventArgs args)
{
    mydelegate handler = myevent;
    if(handler != null) handler(this, args);
}

The reason for doing it this way is that you should always make sure that there is at least one handler attached to the event. There is a small chance that the handler can be detached between checking for null and calling the handler, so it's good to pass it on to another variable which it cannot be detached from before checking for null.

Upvotes: 1

Marcelo Cantos
Marcelo Cantos

Reputation: 186098

You have declared mydelegate to require an object and an EventArgs, so you have to pass these in. Of course, you don't have declare your delegate with these arguments. You could just declare it thus:

public delegate void mydelegate();

or just pass this as the sender:

public delegate void mydelegate(object sender);

Upvotes: 0

Related Questions