Domanda

I've got an delegate and event with an out parameter:

public delegate void ExampleDelegate(object sender, EventArgs e, out string value);

public event ExampleDelegate Example;

When I'm trying to handle the event:

 mg.Example += (sender, e, val) =>
 {
    //do stuff
 };

I'm getting the error Parameter 3 must be declared with the 'out' keyword

When I'm throwing in the suggested out keyword like so:

 mg.Example += (sender, e, out val) =>
 {
    //do stuff
 };

I'm getting and extra error the type of namespace name 'val' could not be found..etc

What am I doing wrong?

È stato utile?

Soluzione

Your event handler doesn't confirm to the .net guidelines.

If you must use it like that, use a delegate, not an event.

You would run into trouble if you had two event handlers modifying your out parameter.

Refer: Events Tutorial

.NET Framework Guidelines

Although the C# language allows events to use any delegate type, the .NET Framework has some stricter guidelines on the delegate types that should be used for events. If you intend for your component to be used with the .NET Framework, you probably will want to follow these guidelines.

The .NET Framework guidelines indicate that the delegate type used for an event should take two parameters, an "object source" parameter indicating the source of the event, and an "e" parameter that encapsulates any additional information about the event. The type of the "e" parameter should derive from the EventArgs class. For events that do not use any additional information, the .NET Framework has already defined an appropriate delegate type: EventHandler.

zmbq has already given you the answer to how to correct your error.

I am adding this just for completeness.

Altri suggerimenti

Well, as it clearly says here, you need to specify the type of val:

(sender, e, out string val)=> ...
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top