C300ks
C300ks

Reputation: 31

VB Event Handling (conversion from .NET) from a given API

So I am stuck with a problem of trying to integrate an API with example code written in .NET into legacy code written in VB and the problem I am having is with Events. The example app given by with the API is using forms, however I am trying to repeat this in a console app while firing off events. There are many different event calls that I need to achieve but here is one example: .NET call:

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

And here is the call

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);

        }

I am not used to programming in VB but here is my attempt to replicate in 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

Now I am not sure how to actually fire the event Any ideas? Here is the full vb code I have currently

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

Upvotes: 1

Views: 78

Answers (1)

Étienne Laneville
Étienne Laneville

Reputation: 5031

There is no AddHandler in VB6. You have to declare your variable using WithEvents (which you have done) and you can add an event handler from the IDE directly using the dropdowns at the top of the code window. An empty event handler will get generated and will be named objectname_eventname. What you currently have, Public Sub Conn_BaseOnLine(ByVal BaseID As Integer, ByVal BaseState As Integer), looks good but you can double-check that the IDE is picking it up as the event handler for your object.

Take a look at this question for some extra details.

Upvotes: 1

Related Questions