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 8764071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:56:19+00:00 2026-06-13T15:56:19+00:00

We are converting a VB6 application to C# (4.0). and have come across a

  • 0

We are converting a VB6 application to C# (4.0). and have come across a method in VB6 that we’re battling to understand.

Public Sub SaveToField(fldAttach As ADODB.Field)
    Dim bData() As Byte
    Dim nSize As Long

    nSize = Len(m_sEmail)
    bData = LngToByteArray(nSize)
    fldAttach.AppendChunk bData

    If nSize > 0 Then
        bData = StringToByteArray(m_sEmail)
        fldAttach.AppendChunk bData
    End If

    nSize = Len(m_sName)
    bData = LngToByteArray(nSize)
    fldAttach.AppendChunk bData
    If nSize > 0 Then
        bData = StringToByteArray(m_sName)
        fldAttach.AppendChunk bData
    End If

    bData = LngToByteArray(m_nContactID)
    fldAttach.AppendChunk bData

End Sub

It seems like it’s doing some binary file copy type thing, but I’m not quite understanding. Could someone explain so that we can rewrite it?

  • 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-13T15:56:22+00:00Added an answer on June 13, 2026 at 3:56 pm

    It serializes the members of the current class (m_sEmail, m_sName, etc.) into the fldAttach database field as a byte array. Each data element is prefixed with its size, that’s why th code LngToByteArray(nSize) is there for each piece of data written out.

    In C# you’d use a MemoryStream and BinaryWriter to accomplish the serialization aspect of it and then you’d write the byte array from the memory stream to the database. Something like:

    byte[] byData;
    
    using (MemoryStream oStream = new MemoryStream)
    {
        using (BinaryWriter oWriter = new BinaryWriter (oStream))
        {
            if (m_sName == null)
            {
                oWriter.Write ((byte) 0); // null string
            }
            else
            {
                oWriter.Write ((byte) 1); // not a null string
                oWriter.Write (m_sName);
            }
    
            // other fields
        }
    
        byData = oStream.ToArray (); // get serialized byte array
    }
    
    // use byData here
    

    Edit: MarkJ pointed out in the comments that this code doesn’t write the same binary format as the original VB6 code, only something similar. If there’s an existing database with records written by the VB6 code, the C# code will have to handle those.

    There are two major differences: one is how strings are output – the original VB6 code doesn’t deal with null strings (there’s no such concept in VB6). The other is that BinaryWriter.Write(string) automatically writes a length-prefixed string – this may or may not be in the same exact format used by the VB6 code which outputs a length and then the string bytes. C# can replicate the VB6 code here using the following logic:

    ...
    
    // assuming sStr is not null
    byte[] byString = Encoding.Unicode.GetBytes ( sStr );
    
    oWriter.Write ( sStr.Length );
    oWriter.Write ( byString );
    

    The C# port will have to assume no null strings or it’ll have to deal with those in some way.

    It may be better to write a small utility that goes through the database and updates all records to the new format in which strings have a null marker, a length-prefix and then the string bytes. I’d personally go with this solution since then new code doesn’t have to deal with the quirks of and old language.

    PS:

    Do note that Long in VB6 maps to int in C#. This is important for handling the length prefix for the VB6 binary format.

    Also, here’s a link to a question about VB6 strings – this may be useful when porting happens:

    VB6 equivalent of string.IsNullOrEmpty

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a VB6 application that I am converting to .Net in phases. The
I have some VB6 code that I am converting to VB.net and came across
I have a VB6 application that I am converting to .net. I am doing
I'm converting a VB6 application to VB.Net that draws on picture boxes. Naturally I
I have a legacy VB6 application that was built using MSDE. As many client's
I am converting an application originally written in vb6 to vb.net. One of the
I am converting my company's VB6 program over to VB.net and I have hit
I am converting a VB6 project to C# .Net but I have problems using
I have been tasked with converting an old VB6 program to to C#. One
I am having trouble converting some code from VB6 to VB.NET (I don't have

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.