查找发件人而不将其作为.NET中的参数传递

时间:2013-05-05 10:02:55

标签: .net vb.net

我正在使用与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对象的情况下触发了事件?

提前感谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

vb语法让我感到厌烦,但似乎你可以为每个终端添加一个不同的处理程序。因此,向终端i添加一个处理程序,将其终端(i)传递给作为发送方的函数。

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