Question Convert C to vbnet

PieterSO

New member
Joined
Mar 15, 2021
Messages
1
Programming Experience
5-10
I have an existing vbnet program. With this I want to make a connection with a SSE server send events from openhab.
I have found a basic program but it is in C, so I'm trying to convert it to vb.
I don' have error anymore in studio but it doesn't run.
It stops with the line:
Using StreamReader As New StreamReader(Await client.GetStreamAsync(url))

Anyone an Idea. Or maybe a starting point for vbnet ? Or an example in vbnet?

C:
C#:
namespace ConsoleApp2
{
    class Program
    {
        static async Task Main(string[] args)
        {
            HttpClient client = new HttpClient();
            client.Timeout = TimeSpan.FromSeconds(5);
            string url = $"http://localhost:8080/rest/events";
            while (true)
            {
                try
                {
                    Console.WriteLine("Establishing connection");
                    using (var streamReader = new StreamReader(await client.GetStreamAsync(url)))
                    {
                        while (!streamReader.EndOfStream)
                        {
                            var message = await streamReader.ReadLineAsync();
                            Console.WriteLine($"Received message: {message}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    //Here you can check for
                    //specific types of errors before continuing
                    //Since this is a simple example, i'm always going to retry
                    Console.WriteLine($"Error: {ex.Message}");
                    Console.WriteLine("Retrying in 5 seconds");
                    await Task.Delay(TimeSpan.FromSeconds(5));
                }
            }
        }
    }
}

-> vbnet

VB.NET:
Module Program
    Public Sub Main(args As String())
        ReadAndDisplayAsync()
    End Sub

    Async Sub ReadAndDisplayAsync()
        Dim client As HttpClient = New HttpClient()
        client.Timeout = TimeSpan.FromSeconds(5)
        Dim url As String = $"http://localhost:8080/rest/events"
        While (True)
            Try
                Console.WriteLine("Establishing connection")
                'Dim aaa As Stream = Await client.GetStreamAsync(url)
                Using StreamReader As New StreamReader(Await client.GetStreamAsync(url))
                    'Using streamReader As New StreamReader(await client.GetStreamAsync(url))
                    While (Not StreamReader.EndOfStream)
                        Dim message = Await StreamReader.ReadLineAsync()
                        Console.WriteLine($"Received price update: {message}")
                    End While
                End Using
            Catch ex As Exception
                Console.WriteLine("xxxx")
                '//Here you can check for
                '//specific types of errors before continuing
                '//Since this Is a simple example, i'm always going to retry
                Console.WriteLine($"Error: {ex.Message}")
                Console.WriteLine("Retrying in 5 seconds")
                'await Task.Delay(TimeSpan.FromSeconds(5))
                'Task.Delay(TimeSpan.FromSeconds(5))
                'Dim result As String = Await WaitAsynchronouslyAsync()
                WaitAsynchronouslyAsync()
            End Try
        End While
    End Sub
    Public Async Function WaitAsynchronouslyAsync() As Task(Of String)
        Await Task.Delay(10000)
        Return "Finished"
    End Function
End Module
 
Last edited by a moderator:
It is C# not C, search web for C# to VB.Net converter. Also use code button </> when posting code to make it readable.
 
For conversion of C# to VB, I would suggest the use of Instant VB from Tangible Software Solutions. It is easily the best converter out there that I have found and the free version is enough in most cases.
 
Back
Top