« .NET Framework Library Source Code now available | Home | Getting the Current Line Number in Textbox using VB.NET »
How to Create/Make a String Buffer using VB.NET
By admin | January 22, 2008
This is a simple Tip on how to create a string with a specific sized buffer. Unfortunately, the traditional way alot of the VB 5.0 and VB 6.0 programmers used to created a string variable with a specific size buffer as shown below, it 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 used 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 above will make the variable a String with a 128 space Buffer. If your using Visual Basic 6.0 (or 5.0) then use the Space$ Function which will return the value as a 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 5.0/6.0 supports this way of creating a String with a specific sized buffer.
Dim buf As String
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")

------------------------------------------------------------------------------
Thats all you have to do in DotNET to make a String buffer. This code will work in all versions of VB.NET and even Visual Basic 2005, and VB 2008. Obviously you need to set the number of spaces you want to be.
Anyways, hopefully this small little Tip helped
Jason
Topics: - (.NET All + 05/08), - .All VB (Related to All) | No Comments »

