Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7721797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:01:00+00:00 2026-06-01T04:01:00+00:00

How do I convert the following line of code from VB.NET to C#. Dim

  • 0

How do I convert the following line of code from VB.NET to C#.

Dim bytes(tcpClient.ReceiveBufferSize) As Byte

I got the following line from the developerfusion web site, but it’s giving me wrong results in my program.

byte[] bytes = new byte[tcpClient.ReceiveBufferSize + 1];

Here is an example of my entire code in Visual Basic.

Dim tcpClient As New System.Net.Sockets.TcpClient()
TcpClient.Connect(txtIP.Text, txtPort.Text)

Dim networkStream As NetworkStream = TcpClient.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then

    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(txtSend.Text.Trim())

    networkStream.Write(sendBytes, 0, sendBytes.Length)

    ' Read the NetworkStream into a byte buffer.
    TcpClient.ReceiveBufferSize = 52428800 '50 MB

    'Do I need to clean the buffer?
    'Get the string back (response)
    Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    networkStream.Read(bytes, 0, CInt(TcpClient.ReceiveBufferSize))

    ' Output the data received from the host to the console.
    Dim returndata As String = Encoding.ASCII.GetString(bytes)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:01:02+00:00Added an answer on June 1, 2026 at 4:01 am

    Visual Basic specifies the maximum bound of the array instead of the length of the array (arrays start at index 0), so your conversion added an extra byte. In your code however the correct way would be:

    byte[] bytes = new byte[tcpClient.ReceiveBufferSize]; 
    

    If you get wrong results, tell us what exactly is wrong. Maybe it’s another part of the code.

    Edit: Remove the \0 like this:

    byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
    int bytesRead = networkStream.Read(bytes, 0, tcpClient.ReceiveBufferSize);
    // Output the data received from the host to the console. 
    string returndata = Encoding.ASCII.GetString(bytes,0,bytesRead);
    

    Edit: Even better to read the data in packets, so you don’t need to reserve a large buffer upfront:

    byte[] bytes = new byte[4096]; //buffer
    int bytesRead = networkStream.Read(bytes, 0, bytes.Length);
    while(bytesRead>0)
    {
        // Output the data received from the host to the console. 
        string returndata = Encoding.ASCII.GetString(bytes,0,bytesRead);
        Console.Write(returndata);
        bytesRead = networkStream.Read(bytes, 0, bytes.Length);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help from some one to convert the following line of code from
How do you convert the following C# code to VB.NET? private static readonly ICollection<string>
I have the following line of code: LPSTR address = T2A((LPTSTR)hostAddress); Can I convert
I want to save image from chart graphic. I'm using following line. chart.SaveImage(D:\\mypic.png, System.Web.UI.DataVisualization.Charting.ChartImageFormat.Png);
I got the following code from this question: What is the best way to
I am a java devloper and I wish to convert following code to java
How would I convert the following to a VB.NET predicate using Array.Find? Private Function
I'm porting some code from PHP to .NET (I'm not a PHP developer) and
I've taken the following code from a guide so as to test it, and
I have the following line of code to create object to access to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.