Handling fixed-length strings in Visual Basic.NET

G

Guest

I have had to move to Visual Studio.NET Pro. from Visual Basic 4.0 and am now starting to re-write our code. I was a bit surprised to find that Visual Basic.NET no longer supports fixed length strings, which is a pain as we use our own database format with specific, user-defined data types and random access files. An example module content would be

Option Explici
Type Record
Operation As String * 4
unit As String * 2
Marker1 As String *
Size As Intege
Material As String * 1
Massaverage As Doubl
Trippage As Intege
ProductionTotal As Doubl
country As String *
region As String * 1
Year As Intege
End Typ

Type Record
a(1 To 10) As String * 4
m(1 To 10) As Doubl
e(1 To 10, 1 To 3) As Doubl
l(1 To 10) As Intege
End Typ

I would very much appreciate any tips on how I can recode the above data types so that Visual Basic.NET can read existing data files created using these data types

Many thanks in advance for any replies.
 
A

Armin Zingler

LCAdeveloper said:
I have had to move to Visual Studio.NET Pro. from Visual Basic 4.0
and am now starting to re-write our code. I was a bit surprised to
find that Visual Basic.NET no longer supports fixed length strings,
which is a pain as we use our own database format with specific,
user-defined data types and random access files. An example module
content would be:

Option Explicit
Type Record1
Operation As String * 41
unit As String * 25
Marker1 As String * 1
Size As Integer
Material As String * 10
Massaverage As Double
Trippage As Integer
ProductionTotal As Double
country As String * 3
region As String * 10
Year As Integer
End Type

Type Record2
a(1 To 10) As String * 40
m(1 To 10) As Double
e(1 To 10, 1 To 3) As Double
l(1 To 10) As Integer
End Type

I would very much appreciate any tips on how I can recode the above
data types so that Visual Basic.NET can read existing data files
created using these data types.

Have a look at
Microsoft.VisualBasic.VBFixedArrayAttribute
Microsoft.VisualBasic.VBFixedStringAttribute

These are only relevant when using the VB file I/O functions (fileopen,
fileget ...)

Attributes in general:
http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconAttributes.asp

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconextendingmetadatausingattributes.asp


Very important for upgrading in general:
http://msdn.microsoft.com/library/en-us/vbcon/html/vboriUpgradingFromVisualBasic60.asp
especially sub topic "Introduction to Visual Basic .NET for Visual Basic
Veterans"

and
http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconDifferencesBetweenVB6AndVB7.asp

especially concerning your question:
http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconStringLength.asp

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
P

Phill. W

LCAdeveloper said:
our own database format with specific, user-defined data types
and random access files. An example module content would be:
.. . .

Fixed Length Strings are only available through a Compatibility
library which, although it will work perfectly well, is only provided
by Our Friends in Redmond as a "crutch" so that VB [Proper]
Developers can keep their existing code running [that is until they
learn how to do things the "new" way]. ;-)

I would recommend replacing your Types with Classes, and your
[Type] elements with Properties thereof. You then have a choice
- either fix the length of each data element in the Class instance
(a.k.a. Record) as you set each property, or add Read and Write
methods that interpret the physical file data into something easier
to handler in code (e.g. the Write method takes the [variable-
length] strings held within the Object and pads them as required
before writing them to the physical file).

HTH,
Phill W.
 
A

Armin Zingler

Phill. W said:
LCAdeveloper said:
our own database format with specific, user-defined data types
and random access files. An example module content would be:
. . .

Fixed Length Strings are only available through a Compatibility
library which, although it will work perfectly well, is only
provided by Our Friends in Redmond as a "crutch" so that VB
[Proper] Developers can keep their existing code running [that is
until they learn how to do things the "new" way]. ;-)

This is wrong. The Compatibility library is a different one
(Microsoft.VisualBasic.Compatibility.dll: "Microsoft Visual Basic .NET
Compatibility Runtime"). The attributes are a part of the usual VB.Net
library (Microsoft.VisualBasic.dll: "Microsoft Visual Basic .NET Runtime").


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
P

Phill. W

Armin Zingler said:
Phill. W said:
LCAdeveloper said:
our own database format with specific, user-defined data types
and random access files. An example module content would be:
. . .

Fixed Length Strings are only available through a Compatibility
library which, although it will work perfectly well, is only
provided by Our Friends in Redmond as a "crutch" so that VB
[Proper] Developers can keep their existing code running [that is
until they learn how to do things the "new" way]. ;-)

This is wrong. The Compatibility library is a different one
(Microsoft.VisualBasic.Compatibility.dll: "Microsoft Visual Basic .NET
Compatibility Runtime"). The attributes are a part of the usual VB.Net
library (Microsoft.VisualBasic.dll: "Microsoft Visual Basic .NET
Runtime").

Oops!!! You're quite right - My mistake.

Now; where /did/ I get idea from?

Apologies,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top