Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a fixed-length string in VB.NET?

How do i Declare a string like this:

Dim strBuff As String * 256

in VB.NET?

like image 238
Rachel Avatar asked Feb 21 '10 08:02

Rachel


2 Answers

It depends on what you intend to use the string for. If you are using it for file input and output, you might want to use a byte array to avoid encoding problems. In vb.net, A 256-character string may be more than 256 bytes.

Dim strBuff(256) as byte

You can use encoding to transfer from bytes to a string

Dim s As String
Dim b(256) As Byte
Dim enc As New System.Text.UTF8Encoding
...
s = enc.GetString(b)

You can assign 256 single-byte characters to a string if you need to use it to receive data, but the parameter passing may be different in vb.net than vb6.

s = New String(" ", 256)

Also, you can use vbFixedString. I'm not sure exactly what this does, however, because when you assign a string of different length to a variable declared this way, it becomes the new length.

<VBFixedString(6)> Public s As String
s = "1234567890" ' len(s) is now 10
like image 83
xpda Avatar answered Sep 22 '22 07:09

xpda


Try this:

    Dim strbuf As New String("A", 80)

Creates a 80 character string filled with "AAA...."'s

Here I read a 80 character string from a binary file:

    FileGet(1,strbuf)

reads 80 characters into strbuf...

like image 29
Gawie Avatar answered Sep 19 '22 07:09

Gawie