委托从VB6转换为vb.net的错误

时间:2013-07-26 19:30:06

标签: vb.net vb6 delegates

尝试将读卡器从VB6改为vb.net。转换后我收到此错误:

'AddressOf' expression cannot be converted to 'Integer' because 'Integer' is not a delegate type

以下是我正在使用的代码:

Public Sub SetupCallBacks()
    'UPGRADE_WARNING: Add a delegate for AddressOf OnEventDeviceStateChanged Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="E9E157F7-EF0C-4016-87B7-7D7FBBC6EE08"'
    Call MTUSCRADeviceStateChangedNotify(AddressOf OnEventDeviceStateChanged)
    'UPGRADE_WARNING: Add a delegate for AddressOf OnEventCardDataStateChanged Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="E9E157F7-EF0C-4016-87B7-7D7FBBC6EE08"'
    Call MTUSCRACardDataStateChangedNotify(AddressOf OnEventCardDataStateChanged)
End Sub


Public Sub OnEventDeviceStateChanged(ByVal parm As Integer)
    If (gbEvents) Then
        If (glDeviceState <> parm) Then
            glDeviceState = parm
        End If

        Select Case glDeviceState
            Case MTSCRA_STATE_DISCONNECTED
                SetStatus(("OnEventDeviceStateChanged:Disconnected"))
            Case MTSCRA_STATE_CONNECTED
                SetStatus(("OnEventDeviceStateChanged:Connected"))
            Case MTSCRA_STATE_ERROR
                SetStatus(("OnEventDeviceStateChanged:Error"))
        End Select
    End If
End Sub

据我所知,我需要与代表做点什么,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

您需要声明一个委托,如下所示:

Delegate Sub DeviceStateChangedDelegate(ByVal param As Integer)

然后在SetupCallBacks方法内部,您需要创建和调用您的委托,如下所示:

Public Sub SetupCallBacks()
    ' Declare and instantiate the delegate
    Dim MTUSCRADeviceStateChangedNotify As DeviceStateChangedDelegate
    MTUSCRADeviceStateChangedNotify = AddressOf OnEventDeviceStateChanged

    ' Invoke the delegate, passing it 10, which is obviously made up
    MTUSCRADeviceStateChangedNotify.Invoke(10)
End Sub