将C#转换为Vb Net,使用'RaiseEvent'语句引发事件

时间:2012-11-23 07:33:11

标签: c# delegates invoke

我将一个c#类转换为vb net,但是有一些事件无法找出这些代理声明:

    Public Delegate Sub IndentChangedEventHandler(NewValue As Integer)
    Public Delegate Sub MultiIndentChangedEventHandler(LeftIndent As Integer, HangIndent As Integer)
    Public Delegate Sub MarginChangedEventHandler(NewValue As Integer)
    Public Delegate Sub TabChangedEventHandler(args As TabEventArgs)

    Public Event LeftHangingIndentChanging As IndentChangedEventHandler
    Public Event LeftIndentChanging As IndentChangedEventHandler
    Public Event RightIndentChanging As IndentChangedEventHandler
    Public Event BothLeftIndentsChanged As MultiIndentChangedEventHandler

    Public Event LeftMarginChanging As MarginChangedEventHandler
    Public Event RightMarginChanging As MarginChangedEventHandler

    Public Event TabAdded As TabChangedEventHandler
    Public Event TabRemoved As TabChangedEventHandler
    Public Event TabChanged As TabChangedEventHandler

'这是在vb net上转换的函数

Private Sub AddTab(pos As Single)
            Dim rect As New RectangleF(pos, 10.0F, 8.0F, 8.0F)
            tabs.Add(rect)
            If TabAdded IsNot Nothing Then
                TabAdded.Invoke(CreateTabArgs(pos))
            End If
        End Sub

c#上的句子是

if (TabAdded != null)
                TabAdded.Invoke(CreateTabArgs(pos));

调用委托的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

我认为你应该使用Address Of。将方法转换为委托是隐含在C#中。在VB.Net中明确地使用Address Of。转换器忘了这件事。

更多信息请阅读

Strange error in code converted to VB.NET from C#

Method group in VB.NET?

答案 1 :(得分:0)

if (TabAdded != null)
            TabAdded.Invoke(CreateTabArgs(pos));

事件在vb.net中有三个访问者:添加,删除和提升。 C#不支持raise访问器,因此您必须显式测试null。这在vb.net中是不必要的,并且不允许,只需直接使用RaiseEvent语句而不测试Nothing:

Private Sub AddTab(ByVal pos As Single)
    Dim rect As New RectangleF(pos, 10.0F, 8.0F, 8.0F)
    tabs.Add(rect)
    RaiseEvent TabAdded(CreateTabArgs(pos))
End Sub