2013-05-05 30 views
0

我正在使用與RFID終端一起使用的API。當任一終端中觸發一個事件,該API照顧它,並傳遞適當的參數的程序:查找發件人而不將其作爲參數傳遞給.NET

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對象的情況下觸發了該事件?

提前致謝。

+0

我相信你不需要爲這個問題'C#'標籤。 'vb.net'就夠了。 – 2013-05-05 10:05:01

回答

1

VB語法轉義我,但似乎你可以添加一個不同的處理程序到每個終端。因此,添加一個處理程序到終端i,它將終端(i)傳遞給作爲發件人的函數。

達到此目的的最簡單方法可能是lambda表達式。

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 
+0

謝謝,我會試一試... – CubicsRube 2013-05-05 10:19:16

+0

是的,它的工作完美!謝謝(和nvoigts)的解決方案。 – CubicsRube 2013-05-05 10:37:43

相關問題