Sum Of VB Strings Can Exceed 64K in Certain Circumstances

Last reviewed: June 21, 1995
Article ID: Q104554
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0

SUMMARY

In all cases, individual variable length strings have a maximum size of 64K worth of characters. However, the sum of the lengths of multiple strings can exceed 64K in the circumstances described in this article.

MORE INFORMATION

Visual Basic for Windows goes beyond any previous Microsoft Basic product in its flexibility when dealing with string variables. As documented in the "Microsoft Visual Basic Programmer's Guide," Appendix D: Individual strings always have a maximum size of 64K characters. However, this is not an absolute limit when dealing with multiple strings. The sum of the lengths of multiple strings can exceed 64K in the circumstances described below:

  1. Global strings declared at the module level and assigned values elsewhere can each have a value of up to 64K and their total can exceed that. For example, if you have the following module-level declarations:

    Global a as string Global b as string Global c as string

    you could have the following code in a Sub procedure:

    a = Space(64000) b = Space(64000) c = Space(64000)

  2. The sum of all module level variable length strings can exceed 64K. For example, if you have the following module-level declarations:

    Dim a as string Dim b as string Dim c as string

    you could have the following code in a Sub procedure in the same module:

    a = Space(64000) b = Space(64000) c = Space(64000)

  3. The sum of all local variable-length string variables can exceed 64K, but only across different Sub procedures. The limit within a single Sub procedure is 64K for all local variable-length strings. For example, the following code would work correctly:

       Sub MySub1()
          Dim a As String
          Dim b As String
          a = Space(32000)
          b = Space(32000)
       End Sub
    
       Sub MySub2()
          Dim a As String
          Dim b As String
          a = Space(32000)
          b = Space(32000)
       End Sub
    
       This is true even when more than one of the Sub procedures are currently
       active such as when MySub1 is called and it calls MySub2. Both are in
       memory and each has a 64K segment available for local variable-length
       strings.
    
       The following code would not work. It would respond correctly with an
       "Out of String Space" error message because it tries to exceed 64K of
       local variable-length strings.
    
       Sub MySub3()
          Dim a As String
          Dim b As String
          Dim c As String
          a = Space(32000)
          b = Space(32000)
          c = Space(32000)
       End Sub
    
    

  4. The variable-length string elements of a user defined type are individually limited to 64K each, but their sum may exceed 64K. For example, if you have the following module-level declarations:

    Type Test

          a As String
          b As String
          c As String
    
    End Type Dim x as Test

    you can have the following code in a Sub procedure:

    x.a = Space(64000) x.b = Space(64000) x.c = Space(64000)

  5. Assigning more than 64K to an array of variable-length strings causes an "Out of String Space" error.

    For example, if you have the following module-level declaration:

    Dim MyArray(12) as String

    The following code in a Sub procedure would cause an error:

    MyArray(1) = Space(64000) MyArray(2) = Space(64000)

    To solve the problem, dimension the array as type Variant:

    Dim MyArray(12) as Variant

    Then the following Sub procedure code will correctly create two 64K variants tagged as strings.

    MyArray(1) = Space(64000) MyArray(2) = Space(64000)

REFERENCES

"Microsoft Visual Basic for Windows Programmer's Guide," version 3.0, Appendix D, pages 644-647.


Additional reference words: 2.00 3.00
KBCategory: kbprg kbcode
KBSubCategory: PrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.