Simon
Simon

Reputation:

VB.NET API Delegates and Events

I am stuck trying to work out Events and Delegates. I am trying to hook up to an external API which returns events when certain events occur. The original code was written in C#, and I am trying to do this in VB.NET.

Here is a short comment from the API programmers.

"After that, client app should need to wait for one of events: OnLoginComplete or OnLoginFailed In cause of succesfull login, all collections is already populated with session data (order states and fills, account positions), so application could display it on the GUI. If by some reason connection is dropped, OnDisconnected event would be fired immediately, notifying the client that requests couldn't be processed until connection is restored.
On a successfull logon system is ready to process client requests: subscriptions to price feeds and order requests. "

I am trying to get the OnLoginFailed event to fire but are having just too many issues not sure how to do this, spent many hours trying to work it out.

Here is a short note from the API Documentation.

OECClient.OnLoginFailed Event
Raised when connection couldn't be establised, credentials are wrong or client API version is invalid.

public event OnLoginFailedEvent OnLoginFailed;

OnLoginFailedEvent Delegate
Occurs when connection failed to be established

public delegate void OnLoginFailedEvent(
    FailReason Reason
);

And here is my code in VB.net, I feel that I am close but just can't get it.

Imports OEC.API
Imports OEC.CommLib

Public Class OpenECry
    Public Shared OECClient1 As New OECClient()

    Public Event OnLoginFailed()
    Public Delegate Sub OEC_OnLoginFailedEvent(ByVal FailReason As OEC.Data.FailReason)



    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        If txbUsername.Text = Nothing Or txbPassword.Text = Nothing Or txbServer.Text = Nothing Then
            'Connect to the server, if the details are wrong then show the error message in the label.
            MsgBox("One of your required fields are empty, Please try again.", MsgBoxStyle.Critical, "Application Error")
        Else
            'Catch Exception
            '
            Try
                OECClient1.Disconnect()
                OECClient1.Connect(txbServer.Text, txbPort.Text, txbUsername.Text, txbPassword.Text, False)


            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub

End Class

I would really appreciate if someone who has more experience could help me out on this one. All I need is one example so I can do all the other events myself.

Upvotes: 2

Views: 670

Answers (2)

vonfeldj
vonfeldj

Reputation: 27

You should be able to do something like.

Public Sub OnLoginFailure() Handles OECClient1.OnLoginFailedEvent
     'Do something upon failure here...
End Sub

Upvotes: 1

Thunder3
Thunder3

Reputation: 1120

Try making your method public. You shouldn't need the delegates et. al., those are for the C# people who don't have the Handles parameter.

Upvotes: 0

Related Questions