Я хотел бы перевести код с C# на VB.Net. Но я не очень понимаю, что я могу сделать в этот момент. У меня есть 2 сообщения об ошибках.
"Публичное событие DisplayText As DisplayTextEvent" является событием и не может быть вызвано напрямую. Используйте оператор RaiseEvent для вызова события.
как это правильно перевести?
C#
пространство имен C.I
{
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
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
Я хотел бы перевести код с C# на VB.Net. Но я не очень понимаю, что я могу сделать в этот момент. У меня есть 2 сообщения об ошибках. "Публичное событие DisplayText As DisplayTextEvent" является событием и не может быть вызвано напрямую. Используйте оператор RaiseEvent для вызова события. как это правильно перевести? C# пространство имен C.I { [code][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;
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; } } } } [/code]
VB.Net Пространство имен C.I [code] 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
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