Tag Archives: buffer

How to Create/Make a String Buffer using VB.NET and VB 6.0

This is a simple tip on how to create a string buffer with a specific number of spaces. The code in this article will create a string with a 128 spaces or buffer size.

The traditional way a lot of the VB 6.0 programmers used to created a string variable with a specific size buffer as shown below, is not supported in .NET.

'This method works just fine with Classic VB (5.0/6.0), but Visual Basic.NET unfortunately
'does not support this somewhat traditional string buffer code.
Dim buf As String * 128

That code will create the string variable “buf” with a buffer of 128 spaces. IF you try running that code in Visual Basic.NET, then you should get a “End of statement expected.” Error.

There is another way, that I use even with VB 6.0 which will work with VB.NET as well. That is by using the Space (Space$) Function.

Public Function Space(ByVal Number As Integer) As String

The Space() Function will return a String with a buffer size that you specify. If your using Visual Basic 6.0 then use the Space$() Function which will return the value as an actual String. String$ is supported in .NET but the Space() returns a String variable no matter which format you use.

'Visual Basic.NET AND Visual Basic 6.0 supports this way of creating a String with
'a specific sized buffer.
Dim buf AsString

buf = Space(128)
'This should throw a message with the size of the buffered string variable.
MsgBox(Len(buf), MsgBoxStyle.OKOnly, " The size of the String")

Results of the codes above

That’s all you have to do in DotNET and 6.0 to make a String buffer. This code will work in all versions of VB.NET, Visual Basic 2008, VB 2010, and newer.

Anyways, hopefully this small little tip helped 🙂


Revised: 2015