0

I am looking at the old VB.net code

Public Event TagDetected(ByVal t As Tag)
...
RaiseEvent TagDetected(t)

that I am trying to convert to C#. My attempt:

public event EventHandler<Tag> TagDetected;
...
TagDetected(this, t.Clone());

doesn't work and gives me an error:

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

4
  • 1
    How is the event triggered? Commented Nov 10, 2014 at 18:35
  • @Ckrempp Do you mean how the even was raised or you mean what code is ran when the event is raised ? I have no idea what code will be ran when the event is raised (and in fact was explicitly asked not to modify it). I.e it will be handled somewhere behind the scene. Commented Nov 10, 2014 at 18:48
  • I mean what action raises the event, like a mouse click, or key press. Commented Nov 10, 2014 at 18:51
  • It is raised within a function. Commented Nov 10, 2014 at 18:52

3 Answers 3

3

The EventHandler{TEventArgs} delegate is for a function that takes a generic type which derived from the EventArgs class. In your example, the Tag class doesn't derive from EventArgs which is why you're getting an error.

The event keyword doesn't have to be used with either the EventHandler or EventHandler{TEventArgs} delegates, but actually any delegate. In this particular case you can better translate the code as follows:

public event Action<Tag> TagDetected;
...
TagDetected(t.Clone());
Sign up to request clarification or add additional context in comments.

2 Comments

Where is everyone getting the generic aspect from? There is no generic flavor in the original VB code at all.
@DaveDoknjas I only brought it up because of his translation. I believe VB is automatically creating an action delegate in the event decoration in the VB code, which most easily translates to a generic action in C#.
2

I think you meant to write:

public event EventHandler<TagEventArgs> TagDetected;

where TagEventArgs is your own child class of EventArgs, exposing a DetectedTag property. This makes sure that the event handler pattern is tightly observed (i.e. you can always refer to the EventArgs as EventArgs in any handler, not having to know that they're just an int, or in your case, just a tag).

Comments

2

There are 2 forms of an event declaration in VB - one uses an explicit delegate type and the other uses an implicit delegate type - you used the implicit delegate approach. C# only has the explicit delegate type approach. The equivalent C# code is:

public delegate void TagDetectedEventHandler(Tag t);
public event TagDetectedEventHandler TagDetected;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.