Fixed length string

  • Thread starter Thread starter Scotty
  • Start date Start date
S

Scotty

Hi everyone,

Is there an equivalent to vb6 Dim test as String * 40 ?

I have been looking for a solution but no luck until now.

Any help would be greatly appreciated.
 
Scotty said:
Hi everyone,

Is there an equivalent to vb6 Dim test as String * 40 ?

I have been looking for a solution but no luck until now.

Any help would be greatly appreciated.

For which purpose? Trailing blanks in a String cost memory and have no
advantage.

I like the detailled chapters for VB6 users. Maybe you, too:
http://msdn2.microsoft.com/en-us/library/kehz1dz1.aspx

Especially this sub topic:
http://msdn2.microsoft.com/en-us/library/f47b0zy4.aspx

See also the MarshalAsAttribute.


Armin
 
Scotty said:
Is there an equivalent to vb6 Dim test as String * 40 ?

I have been looking for a solution but no luck until now.

There is no 1:1 equivalent available out of the box in VB.NET. I suggest to
describe the scenario in more detail -- maybe there even an easier solution
exists.
 
Armin Zingler said:
For which purpose? Trailing blanks in a String cost memory and have no
advantage.

I imagine the OP is reading or writing to a file that has fixed length
fields, rather than delimiters.

Unicode has screwed up the String data type as us old time BASIC programmers
know it. It used to be that Strings were not only used as a sequence of ASCII
characters, but also as a way of storing binary data.

ASCII was 8-bit, but Unicode is (at least) 16-bit, so now instead of the
one-size-fits-all String, we need to use:

String for text
Byte() for binary data
Char() for fixed length strings

However, the syntax is all different for Char() compared to the old
"String*40". You need to use Array.Copy() instead of MID()="value"

So instead of

Dim strFixed as String*40
strFixed = "This is a fun test"
Mid(strFixed,9, 4) = "not a"
MsgBox(strFixed)

we do this

Dim chrFixed() As Char = Space(40).ToCharArray
Array.Copy("This is a fun test".ToCharArray, 0, chrFixed, 0, 18)
Array.Copy("not a".ToCharArray, 0, chrFixed, 8, 5)
MsgBox(CStr(chrFixed))
 
Back
Top