0

我正在使用与 RFID 终端一起使用的 API。当任何终端触发事件时,API 会处理它并将适当的参数传递给过程:

For i = 0 to NumberOfTerminals - 1
    myTerminals(i).ID = i
    myTerminals(i).API.Connect()
    AddHandler myTerminals(i).API.OnRFID, AddressOf OnRFIDSub
End For

Private Sub OnConnectSub(ByVal RFID As String, ByVal EventTime As String)
    MsgBox(RFID & " : " & EventTime)
End Sub

问题是当事件被触发时,API 没有传递任何属性sendere有没有办法知道哪个终端在没有sender对象的情况下触发了事件?

预先感谢。

4

2 回答 2

2

扩展 nvoigts 的答案

For i = 0 to NumberOfTerminals - 1
    myTerminals(i).ID = i
    myTerminals(i).API.Connect()
    'copy the id as there are problems with closing over iteration variables.
    dim id as integer = i
    'Lamda sub to close over id
    AddHandler myTerminals(i).API.OnRFID, _
        Sub(ByVal RFID As String, ByVal EventTime As String) _ 
            OnRFIDSub(RFID,EventTime,id) 
End For

Private Sub OnConnectSub(ByVal RFID As String, ByVal EventTime As String, 
                         ByVal Sender as Integer) 'Sender now contains the terminal ID
    MsgBox(RFID & " : " & EventTime)
End Sub
于 2013-05-05T10:15:28.453 回答
1

vb 语法使我无法理解,但似乎您可以为每个终端添加不同的处理程序。因此,向终端添加一个处理程序i,将它的终端(i)作为发送者传递给函数。

实现这一点的最简单方法可能是 lambda 表达式。

于 2013-05-05T10:09:13.567 回答