0

I'm currently converting a class from C# to Visual Basic 2010.

Most parts have worked fine until I came to the EventHandlers. I haven't been able to convert the following line of code into Visual Basic, neither by myself nor using a converter:

public event EventHandler<StringEventArgs> ServerMessage = delegate { };

The converters' codes only gives me the error "End of statement expected":

Public Event ServerMessage As EventHandler(Of StringEventArgs) = Sub() 

End Sub

So, does the delegate { } part even need to be translated to Visual Basic code?

If so, how can I convert it properly?

If not, is it enough just using the following:

Public Event ServerMessage As EventHandler(Of StringEventArgs)

?

Servy
  • 202,030
  • 26
  • 332
  • 449
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • `EventHandler(Of UserJoinedEventArgs)` should be `EventHandler(Of StringEventArgs)` (based on your original code). – Brad Christie Feb 06 '15 at 18:45
  • @BradChristie : Whoops, you're right. Copied the wrong line of code. :) – Visual Vincent Feb 06 '15 at 18:46
  • Why are you converting C# to VB? – Dai Feb 06 '15 at 18:53
  • Because I want to use the class with my own Project and be able to modify it faster, as I understand more VB than C#. I also saw @Servy removing the C# tag before, I added it again because I thought I accidentally removed it myself. (: – Visual Vincent Feb 06 '15 at 18:56

1 Answers1

3

VB.NET's RaiseEvent checks to see whether an event has no subscribers before it actually raises the event (it won't throw an exception), so your

Public Event ServerMessage As EventHandler(Of StringEventArgs)

should work.

That is the purpose of setting the delegate equal to delegate{ }; in C# -- to avoid having to check for nulls when an event is raised. Since the event always has an empty method assigned to it, it will not throw an exception.

See: https://stackoverflow.com/a/16712799/864414

Community
  • 1
  • 1
transistor1
  • 2,915
  • 26
  • 42