来自给定API的VB事件处理(从.NET转换)



因此,我遇到了一个问题,即试图将带有用.NET编写的示例代码的API集成到用VB编写的遗留代码中,而我遇到的问题是事件。API提供的示例应用程序正在使用表单,但我试图在控制台应用程序中重复这一点,同时启动事件。我需要实现许多不同的事件调用,但这里有一个例子:.NET调用:

ReplyInteract.BaseConnection conn = new ReplyInteract.BaseConnection();          
conn.BaseOnLine += Conn_BaseOnLine;

这是呼叫

private void Conn_BaseOnLine(int BaseID, int BaseState)
{
string status = "";
switch (BaseState)
{
case 1: status = "Connected:Status 1"; txtCurrentBaseID.Text = BaseID.ToString(); break;
case 0: status = "Connection failed or closed: Status 0"; break;
case -1: status = "Connectiontype not supported:Status -1"; break;
case -2: status = "Invalid Base ID: Status -2"; break;
case -3: status = "No base connected or the usb port is occupied: Status - 3"; break;
case -5: status = "Basestation busy with another application. Please disconnect first";break;
case -6: status = "No Reply Interact Base station. Does not work with the Interact API";break;
default: status = BaseState.ToString(); break;
}
writeDebug("Base ID: " + BaseID.ToString() + " >> Status: " + status);
}

我不习惯用VB编程,但这里是我在VB 中复制的尝试

Dim WithEvents conn As New ReplyInteract.BaseConnection
AddHandler conn.BaseOnLine, AddressOf Conn_BaseOnLine
Public Sub Conn_BaseOnLine(ByVal BaseID As Integer, ByVal BaseState As Integer)
Dim status As String
Select Case BaseState
Case 1 : status = "Connected:Status 1"
Case 0 : status = "Connection failed or closed: Status 0"
Case -1 : status = "Connectiontype not supported:Status -1"
Case -2 : status = "Invalid Base ID: Status -2"
Case -3 : status = "No base connected or the usb port is occupied: Status - 3"
Case -5 : status = "Basestation busy with another application. Please disconnect first"
Case -6 : status = "No Reply Interact Base station. Does not work with the Interact API"
Case Else
status = BaseState.ToString()
End Select
End Sub

现在我不知道如何真正启动活动有什么想法吗?这是我目前的完整vb代码

Module Module1
Dim WithEvents conn As New ReplyInteract.BaseConnection
Dim mg As New ReplyInteract.BaseManage
Sub Main()

While (True)
conn.BaseIP = "200.0.0.251"
Dim BaseOnLine As String
conn.Open(1, "1")
conn.License = "****"
AddHandler conn.BaseOnLine, AddressOf Conn_BaseOnLine
End While
End Sub

Public Sub Conn_BaseOnLine(ByVal BaseID As Integer, ByVal BaseState As Integer)
Dim status As String
Select Case BaseState
Case 1 : status = "Connected:Status 1"
Case 0 : status = "Connection failed or closed: Status 0"
Case -1 : status = "Connectiontype not supported:Status -1"
Case -2 : status = "Invalid Base ID: Status -2"
Case -3 : status = "No base connected or the usb port is occupied: Status - 3"
Case -5 : status = "Basestation busy with another application. Please disconnect first"
Case -6 : status = "No Reply Interact Base station. Does not work with the Interact API"
Case Else
status = BaseState.ToString()
End Select

End Sub

VB6中没有AddHandler。您必须使用WithEvents声明您的变量(您已经这样做了(,并且可以使用代码窗口顶部的下拉菜单直接从IDE添加事件处理程序。将生成一个空的事件处理程序,并将其命名为objectname_eventname。您目前拥有的Public Sub Conn_BaseOnLine(ByVal BaseID As Integer, ByVal BaseState As Integer)看起来不错,但您可以仔细检查IDE是否将其用作对象的事件处理程序。

看看这个问题,了解更多细节。

相关内容

  • 没有找到相关文章

最新更新