Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Exceeding the 65,536 character string limit?


Reply
 
Thread Tools Display Modes
  #1  
Old 04-20-2012, 04:07 PM
sscheinfe's Avatar
sscheinfe sscheinfe is offline
Centurion
 
Join Date: Mar 2004
Location: New York City
Posts: 108
Default Exceeding the 65,536 character string limit?


This may be a simple question (been out of VB a long time) but...I need to store more than 65,536 charaters to a string. At the moment, I have them in a textbox, which oddly circumvents the problem...but massively slow the program down.

Is there a way to beat the string limit?
Reply With Quote
  #2  
Old 04-20-2012, 05:45 PM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 6,383
Default

a) Use an array of strings?

b) VB.NET strings can hold approx 2 Billion characters

Why on earth would you need to load so many characters into memory?
Does this question pertain to searching text?
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #3  
Old 04-20-2012, 09:07 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,911
Default

Legacy VB6 also support strings of up to 2^31 characters, assuming you had enough memory.
The textbox control technically is only designed to hold up to 64K worth of text.
Code:
Option Explicit

Dim a As String


Private Sub Form_Load()
  a = Space$(1000000)
  Text1.Text = a
  Debug.Print Len(Text1.Text)
End Sub
This code loads a string with a million spaces.
It assigns the string to a textbox.
It prints out the length of the Text property of the textbox.
The print will say 65535.
You could scroll through all 1 million characters, in the textbox, but if you did something like TextBox1.SelStart = 80000, you would get an exception.
So, while the control is designed to support 64K characters, because it is using a String for the Text property, the String itself (thus the Text property) can hold more characters then the contol is designed to handle, and if not careful, you can have problems.
You really shouldn't use the textbox to hold more characters than it is designed for.

Now, you probably thought the string was limited to 65,535 characters because your are defining a fixed length string, i.e.

Dim b as String*65535

and fixed length strings are limited to 65535 characters.
A fixed length string is allocated once in local process memory, and never changes size.
If you assign a short string to it, it is padded out with spaces.
If you use a dynamic string, it is allocated out on the heap and the size of the string is however many characters you assigned to it.
If you assign more or less characters to the string, the string expands or shrinks (usally by reallocating new space for the new sized string, and garbage collecting the old space later).
The dynamic string can be whatever size, up to 2^31 characters, that your memory will support.
We have plenty of examples, where a string is sized to match the size of a file, then the whole file is read into the string in one shot.

Sizing a string to a reasonable size buffer, then not modifying it's size, but manually keep track of how many characters you put in the buffer using the Mid$ statement on the left size of an assignment is a very efficient way to manipulate or add to a string, without inccuring the overhead of the string reallocation that is done when you change the size of the string through things like concatenation.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 04-21-2012 at 06:33 PM.
Reply With Quote
  #4  
Old 04-21-2012, 08:41 AM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 6,383
Default

Thnk You for expaining that Passel. You have the a rare gift of putting things in easy to understand terms. I learned something new.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->