【问题标题】:Delegate error converting from VB6 to vb.net从 VB6 转换到 vb.net 的委托错误
【发布时间】:2013-07-27 04:05:45
【问题描述】:

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

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

【问题讨论】:

    标签: vb.net vb6 delegates


    【解决方案1】:

    你需要声明一个委托,像这样:

    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
    

    【讨论】:

    • 感谢您的回复,这确实解决了这个问题,但我卑微的实习生没有意识到使用这种方法读取 HID 需要一个不会在函数中使用的 .dll 文件它将属于...
    • @user2620509 MTUSCRACardDataStateChangedNotify 在外部 DLL 中吗? IE。它是由Declare 语句定义的吗?在这种情况下,我认为这个答案不适用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多