1

试图将读卡器从 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

据我所知,我需要与代表做一些事情,但我不知道该怎么做。

4

1 回答 1

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
于 2013-07-26T20:05:45.603 回答