EventHandler C# Conversion

AlexRogan

Member
Joined
Jan 15, 2010
Messages
5
Programming Experience
Beginner
Could someone help me convert this C# code to VB.NET?

VB.NET:
public event EventHandler Activated;

private void OnActivated()
{
    EventHandler activatedHandler = this.Activated;
    if (activatedHandler != null)
    {
        activatedHandler(this, EventArgs.Empty);
    }
}
Thanks in advance!
*
 
I knew about the conversion sites. But when converted, the following error occurs:

Visual Studio 2010 Error List said:
'Public Event Activated(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

Can anyone else give it a shot?

Thanks!
 

Attachments

  • OnActivated.png
    OnActivated.png
    13.6 KB · Views: 35
VB.NET:
    If activatedHandler IsNot Nothing Then
        RaiseEvent activatedHandler(Me, EventArgs.Empty)
    End If
 
:confused: You just reposted what you posted in your first post. :confused:

As you see from the screenshot, the first line of the procedure is the offending one:
VB.NET:
Dim activatedHandler As EventHander = Me.Activated

The 'Me.Activated' part is squiggly underlined and as previously stated, the error is:

'Public Event Activated(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
 
check both posts, obviously the line in error should be taken out via the screenshot, then in my 2nd post in this thread it has the RaiseEvent keyword which isn't in my first post.
 
If you remove the first line (as you show in post #4) then activatedHandler won't be instantiated and will always equal nothing. Then you'll get an error when trying to call RaiseEvent (activatedHandler will always equal nothing so the RaiseEvent will never be called). When and how do you declare and instantiate activatedHandler?

obviously the line in error should be taken out via the screenshot
I'm not really sure what that means.
 
In VB.Net events are normally raised with the RaiseEvent statement:
VB.NET:
Public Event Activated As EventHandler

Private Sub OnActivated()
    RaiseEvent Activated(Me, EventArgs.Empty)
End Sub
This is the complete conversion of that code, you don't need anything else to achieve what that code does.
 
Thanks John. While waiting for an answer, I did some reading on Events in C#. Your reply was what I was thinking, the conformation was appreciated.
 

Latest posts

Back
Top