locked
LenB function no longer supported, What replaced it? RRS feed

  • Question

  • I have a VB 6 project which has a form that uses the Lenb function.  When I tried to convert the program to VB 2005 I recieved the error that the name LenB was not declared.  Looking at the error, it turns out that the function is no longer supported!  The code looks like this:

     

    Private Sub cmdStore_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdStore.Click

    ' initialize

    MyDate = Today

    'UPGRADE_ISSUE: LenB function is not supported. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'

    mudtMortgage = LenB(MyMortgage)

    lblError.Text = ""

    dlgStoreOpen.FileName = ""

    On Error GoTo OpenError

    dlgStoreOpen.ShowDialog()

    Filename = dlgStoreOpen.FileName

    FileOpen(1, Filename, OpenMode.Random, OpenAccess.Write, , mudtMortgage)

    'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'

    FilePut(1, MyDate, 1)

    'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'

    FilePut(1, Record_Name.Value, 2)

    'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'

    FilePut(1, MyMortgage, 3)

    FileClose(1)

    cmdStore.Enabled = False

    Exit Sub

    OpenError:

    lblError.Text = "Please enter a file name to store."

    cmdStore.Enabled = True

    End Sub

     Please Help!!!!

    Thank you

    Saturday, July 7, 2007 4:01 AM

Answers

  • It's just Len() without the B

     

    Adamus

    Saturday, July 7, 2007 4:06 AM

All replies

  • It's just Len() without the B

     

    Adamus

    Saturday, July 7, 2007 4:06 AM
  • Thanks, that was simple.......
    Monday, July 9, 2007 6:39 PM
  • Actually, it's not the same as Len.  I tried them both in VB 6 and LenB returns a value that is twice that of Len.  So you will have to multiply whatever you get with Len by 2.

     

     

    Monday, July 9, 2007 11:40 PM
  •  

    That depends on whether the question is characters or bytes.

     

    Of course there is also String.Length which is a character count.

    Tuesday, July 10, 2007 12:02 AM
  • A VB6 program would often use LenB() to get the actual byte length of a string or structure, usually to provide it to a P/Invoke or to insert the length of a structure into its first member and then provide that to a P/Invoke. Code using LenB() is always flagged as not supported in VB.NET. Although Microsoft’s help is lacking in providing a clear solution to this issue, I have found a resolution. Though many online gurus will advise you that for Structures, you should look to the Marshal Class (a part of the System.Runtime.InteropServices namespace), which has a SizeOf() method that will accept objects, such as structures, and will return its size in bytes. This seems to be exactly what we need. However, in actual testing I found that the result does not accommodate string data, except as 4-byte IntPtr types (Integer Pointers). So the OSVERSIONINFO structure described in the last point would return a value of 24, which accounts for 5 Integers, plus 1 IntPtr referencing the string data ((5+1) x 4). But I understand, because they also said the VB.NET Len() function did not support Structures (misinformation originating from Microsoft). But that was only true for early releases. VB.NET’s Len() function now supports Structures and will return its byte size. For Strings, you can either double the value of the string’s Length property (same as using the Len() function), or you can use the GetByteCount property of the System.Text namespace’s Unicode.Encoding class.

    Consider the following upgraded VB6 code:

    'UPGRADE_ISSUE: LenB function is not supported. 'Click for more: 'ms-help://MS.VSCC.v90/... BLAH BLAH

    CopyMemory(MnMxInfo, lParam, LenB(MnMxInfo)) 'get structure byte size

    'UPGRADE_ISSUE: LenB function is not supported. 'Click for more: 'ms-help://MS.VSCC.v90/... BLAH BLAH

    Dim iLen As Integer = LenB(myString)         'get string byte size

    We can easily fix the above two lines by updating them to:

    CopyMemory(MnMxInfo, lParam, Len(MnMxInfo))                               'get structure byte size

    Dim iLen As Integer = System.Text.Encoding.Unicode.GetByteCount(myString) 'get string byte size

    Or better, the above fix can be eliminated and the original code will transform into viable code simply by adding our own overloaded LenB functionality. In a module, just add these two functions:

    Module modLenB

        '********************************************************

        ' Provide the VB6 LenB Functionality – Type-Safe approach

        '********************************************************

        Public Function LenB(ByVal ObjStr As String) As Integer

        'Note that ObjStr.Length will fail if ObjStr was set to Nothing   

        If Len(ObjStr) = 0 Then Return 0

            Return System.Text.Encoding.Unicode.GetByteCount(ObjStr)

        End Function

     

        Public Function LenB(ByVal Obj As Object) As Integer

            If Obj Is Nothing Then Return 0

            Try 'Structure

                Return Len(Obj)

            Catch 'Leave blank for catch-all

                Try 'Type-def objects

                    Return System.Runtime.InteropServices.Marshal.SizeOf(Obj)

                Catch 'Leave blank for catch-all

                    Return -1 'Allow user to check for <0 as error

                End Try

            End Try

        End Function

    End Module

     

     

     

     

    Note: The above is a clip from a 48-page VB6 to VB.NET upgrade helpsheet I am developing to help users quickly get their upgraded VB6 code to being fully functional in VB.NET in very short order. So far I have been able to get rather large application test-ready in a couple of hours.
    • Proposed as answer by Collin Sauve Wednesday, January 13, 2010 10:42 PM
    • Edited by DavidRossG Saturday, January 23, 2010 5:30 AM updated, corrected info
    Monday, November 23, 2009 3:05 AM
  • I think it's beyond the time that we put vb6 to sleep where it belongs and quit using it's functions.

    Renee
    Monday, November 23, 2009 3:35 AM
  • Thanks for posting this. 

    Appears that I am upgrading the same bit of code...one of our Network folks needs to create a dial up networking connection programmatically and found some VB 6.0.  Now, I'm working to get it into .NET.  Do you know if this conversion has already been completed and where it would be available?

    Thanks again,
    Johanna Aqui
    Friday, February 5, 2010 4:21 PM
  • To which conversion do you refer?
    Renee
    Friday, February 5, 2010 6:47 PM
  • Converting VB 6.0 code to VB.NET for calls to the CopyMemory function.  I need to create a dail up connection but have only been able to find VB 6.0 code samples and converting the CopyMemory function for use in .NET is difficult.  I've found the following article very useful in understanding and resolving this and was wondering if someone had already gone through this process.

    http://www.codeproject.com/KB/vb/CopyMemory_in_Net.aspx

    Johanna Aqui
    Friday, February 5, 2010 7:20 PM
  • "Note: The above is a clip from a 48-page VB6 to VB.NET upgrade helpsheet I am developing to help users quickly get their upgraded VB6 code to being fully functional in VB.NET in very short order. So far I have been able to get rather large application test-ready in a couple of hours.
    • Proposed As Answer by Collin Sauve Wednesday, January 13, 2010 10:42 PM
    • Edited by DavidRossG Saturday, January 23, 2010 5:30 AM updated, corrected info"

     

    I would be very interested in the 48page help sheet. If it is online could you point me in the right direction. If not maybe you can contact me through this board.

    Any help would be appreciated

    Tuesday, February 22, 2011 5:54 PM