I would like to translate code from C# to VB.Net. But I don't really understand what I can do at this point. I have 2 error messages.
"Public Event DisplayText As DisplayTextEvent" is an event and cannot be called directly. Use a RaiseEvent statement to invoke an event.
how do I translate that correctly?
C#
namespace C.I
{
[Serializable]
public class DisplayTextEventArgs
{
public string Text { get; set; }
public TimeSpan Duration { get; set; }
public DisplayTextEventArgs(string text, TimeSpan duration)
{
Text = text;
Duration = duration;
}
public override string ToString()
{
return String.Format("{0}", Text);
}
}
[Serializable]
public delegate void DisplayTextEvent(DisplayTextEventArgs args);
public class CaptureInterface : MarshalByRefObject
{
public event DisplayTextEvent DisplayText;
private void SafeInvokeDisplayText(DisplayTextEventArgs displayTextEventArgs)
{
if (DisplayText == null)
return; //No Listeners
DisplayTextEvent listener = null;
Delegate[] dels = DisplayText.GetInvocationList();
foreach (Delegate del in dels)
{
try
{
listener = (DisplayTextEvent)del;
listener.Invoke(displayTextEventArgs);
}
catch (Exception)
{
//Could not reach the destination, so remove it
//from the list
DisplayText -= listener;
}
}
}
}
}
VB.Net
Namespace C.I
<Serializable>
Public Class DisplayTextEventArgs
Public Property Text As String
Public Property Duration As TimeSpan
Public Sub New(text As String, duration As TimeSpan)
Me.Text = text
Me.Duration = duration
End Sub
Public Overrides Function ToString() As String
Return String.Format("{0}", Text)
End Function
End Class
<Serializable>
Public Delegate Sub DisplayTextEvent(args As DisplayTextEventArgs)
Public Class CaptureInterface
Inherits MarshalByRefObject
Public Event DisplayText As DisplayTextEvent
Private Sub SafeInvokeDisplayText(displayTextEventArgs As DisplayTextEventArgs)
If DisplayText Is Nothing Then
Return ' No Listeners
End If
Dim listener As DisplayTextEvent = Nothing
Dim dels As [Delegate]() = DisplayText.GetInvocationList()
For Each del As [Delegate] In dels
Try
listener = CType(del, DisplayTextEvent)
listener.Invoke(displayTextEventArgs)
Catch ex As Exception
' Could not reach the destination, so remove it from the list
RemoveHandler DisplayText, listener
End Try
Next
End Sub
Public Sub DisplayTextProxyHandler(args As DisplayTextEventArgs)
RaiseEvent DisplayText(args)
End Sub
End Class
End Namespace
If you're not raising the event, in VB you need to refer to the hidden event field, which is the event name followed by 'Event'. (Strange - I know).
If DisplayTextEvent Is Nothing Then
Return 'No Listeners
End If
Dim listener As DisplayTextEvent = Nothing
Dim dels() As System.Delegate = DisplayTextEvent.GetInvocationList()