user310291
user310291

Reputation: 38220

Can't overload Delegate for Custom Event?

i want to create

    public delegate void ValueChangedHandler(int value);
    public delegate void ValueChangedHandler(Object sender, int value);

it refuses: how to do so or is it impossible ?

EDIT: Thanks for the answer, I understand the technical reason, but still what I want to do makes sense from expressivity point of view so I'm upset that .net framework doesn't have a way to do so.

Upvotes: 2

Views: 663

Answers (4)

Piotr Justyna
Piotr Justyna

Reputation: 4976

Since ValueChangedHandler is not a method, but a type, you can't overload it - you can instead create a new event handler with different parameters.

The common practice is also to extend paraterers of an event handler:

public delegate void EventHandler(object sender, EventArgs e);

You can define your custom class extending EventArgs to pass more data.

Upvotes: 1

dhinesh
dhinesh

Reputation: 4764

A delegate is a type. i.e if you are creating a delegate then you are actually making a class which is derived from System.Delegate.

public delegate void ValueChangedHandler(int value);

So by this you have created a class ValueChangedHandler. So again if you are writing

public delegate void ValueChangedHandler(int value, int j);

then it is two classes with same name under a single namespace. So the compiler will not allow.

Upvotes: 1

Thomas Levesque
Thomas Levesque

Reputation: 292555

You're confusing delegates and methods... Methods can be overridden, delegates can't.

A delegate is a type, you can declare a variable of type ValueChangedHandler:

ValueChangedHandler handler;

If you could overload delegates, to which overload of ValueChangedHandler would this code refer ?

Overloading delegates just wouldn't make sense...

Upvotes: 4

LaGrandMere
LaGrandMere

Reputation: 10359

I'm not sure that you can overload delegates, never seen it before.

What I would suggest would be something like this :

public delegate void ValueChangedHandler(params object[] list);

Hope this helps,

Upvotes: 1

Related Questions