Microsoft KB Archive/104554

= Sum Of VB Strings Can Exceed 64K in Certain Circumstances =

Article ID: 104554

Article Last Modified on 12/9/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q104554



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:

  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)   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)   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   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)   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) 

