By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,504 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,504 IT Pros & Developers. It's quick & easy.

Handling fixed-length strings in Visual Basic.NET

P: n/a
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.
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"LCAdeveloper" <an*******@discussions.microsoft.com> schrieb
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...Attributes.asp

http://msdn.microsoft.com/library/en...attributes.asp
Very important for upgrading in general:
http://msdn.microsoft.com/library/en...ualBasic60.asp
especially sub topic "Introduction to Visual Basic .NET for Visual Basic
Veterans"

and
http://msdn.microsoft.com/library/en...nVB6AndVB7.asp

especially concerning your question:
http://msdn.microsoft.com/library/en...ringLength.asp

--
Armin

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

Nov 20 '05 #2

P: n/a

"LCAdeveloper" <an*******@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
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.
Nov 20 '05 #3

P: n/a
"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> schrieb

"LCAdeveloper" <an*******@discussions.microsoft.com> wrote in
message news:0B**********************************@microsof t.com...
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

Nov 20 '05 #4

P: n/a
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40*********************@news.freenet.de...
"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> schrieb

"LCAdeveloper" <an*******@discussions.microsoft.com> wrote in
message news:0B**********************************@microsof t.com...
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.
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.