Avatar of Darwin Johnson
Darwin JohnsonFlag for United States of America asked on

Trying to convert from C# to vb.net

Trying to convert the below code to vb.net. It looks like I need to setup events not sure what the += is doing.


        public MainForm()
        {
            InitializeComponent();

            KRserver.ResponseDepositStart += KRserver_ResponseDepositStart;
            KRserver.ResponseDepositComplete += KRserver_ResponseDepositComplete;
            
        }


        private void KRserver_ResponseDepositStart(DepositStartResponse depositStart)
        {
            this.Invoke(new Action(delegate ()
            {
                txtBoxReceive.AppendText("\r\n===== Response - Deposit Start ======\r\n");
                txtBoxReceive.AppendText($"Result: {depositStart.result}\r\n\r\n");
            }));
        }

        private void KRserver_ResponseDepositComplete(DepositCompleteResponse depositComplete)
        {
            this.Invoke(new Action(delegate ()
            {
                txtBoxReceive.AppendText("\r\n===== Response - Deposit Complete =====\r\n");
                txtBoxReceive.AppendText($"Result: {depositComplete.result}\r\n\r\n");
            }));
        }

C#Visual Basic.NET

Avatar of undefined
Last Comment
it_saige

8/22/2022 - Mon
David Johnson, CD

you can mix and match .cs and .vb files in a solution.
https://converter.telerik.com/

Class SurroundingClass
    Public Sub New()
        InitializeComponent()
        KRserver.ResponseDepositStart += AddressOf KRserver_ResponseDepositStart
        KRserver.ResponseDepositComplete += AddressOf KRserver_ResponseDepositComplete
    End Sub

    Private Sub KRserver_ResponseDepositStart(ByVal depositStart As DepositStartResponse)
        Me.Invoke(New Action(Function()
                                 txtBoxReceive.AppendText(vbCrLf & "===== Response - Deposit Start ======" & vbCrLf)
                                 txtBoxReceive.AppendText($"Result: {depositStart.result}\r\n\r\n")
                             End Function))
    End Sub

    Private Sub KRserver_ResponseDepositComplete(ByVal depositComplete As DepositCompleteResponse)
        Me.Invoke(New Action(Function()
                                 txtBoxReceive.AppendText(vbCrLf & "===== Response - Deposit Complete =====" & vbCrLf)
                                 txtBoxReceive.AppendText($"Result: {depositComplete.result}\r\n\r\n")
                             End Function))
    End Sub
End Class

Open in new window

Kyle Abrahams

ASKER
Darwin Johnson

Here is what I had tried and I get the below error at design time

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim KRserver As New KRPosServer

        AddHandler KRserver.DepositStartHandler, AddressOf KRserver_ResponseDepositStart
       
    End Sub

Private Sub KRserver_ResponseDepositStart(ByVal depositStart As DepositStartResponse)
        Me.Invoke(New Action(Function()
                                 txtBoxReceive.AppendText(vbCrLf & "===== Response - Deposit Start ======" & vbCrLf)
                                 txtBoxReceive.AppendText($"Result: {depositStart.result}\r\n\r\n")
                             End Function))
    End Sub

Severity   Code   Description   Project   File   Line   Suppression State
Error   BC30108   'KRPosServer.DepositStartHandler' is a type and cannot be used as an expression.   KR10TestApp   C:\Users\User\source\repos\KR10TestApp\Form1.vb   15   Active
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
Darwin Johnson

Here is what I get now

Imports KR.Server

    Public Class Form1
    Private KRserver As New KRPosServer

    Public Sub New()
        InitializeComponent()
        KRserver.ResponseDepositStart += AddressOf KRserver_ResponseDepositStart
        KRserver.ResponseDepositComplete += AddressOf KRserver_ResponseDepositComplete
    End Sub

   
    Private Sub KRserver_ResponseDepositStart(ByVal depositStart As DepositStartResponse)
        Me.Invoke(New Action(Function()
                                 txtBoxReceive.AppendText(vbCrLf & "===== Response - Deposit Start ======" & vbCrLf)
                                 txtBoxReceive.AppendText($"Result: {depositStart.result}\r\n\r\n")
                             End Function))
    End Sub

    Private Sub KRserver_ResponseDepositComplete(ByVal depositComplete As DepositCompleteResponse)
        Me.Invoke(New Action(Function()
                                 txtBoxReceive.AppendText(vbCrLf & "===== Response - Deposit Complete =====" & vbCrLf)
                                 txtBoxReceive.AppendText($"Result: {depositComplete.result}\r\n\r\n")
                             End Function))
    End Sub

End Class

Severity   Code   Description   Project   File   Line   Suppression State
Error   BC32022   'Public Event ResponseDepositStart As KRPosServer.DepositStartHandler' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.   KR10TestApp   C:\Users\User\source\repos\KR10TestApp\Form1.vb   11   Active


ASKER CERTIFIED SOLUTION
it_saige

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question