Equivalent of "Dim As String * 1" VB6 to VB.NET
Asked Answered
T

5

8

I have some VB6 code that needs to be migrated to VB.NET, and I wanted to inquire about this line of code, and see if there is a way to implement it in .NET

Dim strChar1 As String * 1

Intellisense keeps telling me that an end of statement is expected.

Toiletry answered 6/3, 2012 at 6:32 Comment(7)
What you have there is a fixed length string. How to declare one in VB.NET is discussed here.Kimmy
@Jay: I saw that too, but personally I would shy away from VBFixedStringAttribute. It's one of the many things in the Microsoft.VisualBasic namespace that was a carry-over from VB6 to make converting to VB.NET easier when .NET first came out, but IMHO it's almost always better to avoid most of the stuff in that namespace and opt for the equivalent "standard" .NET Framework functionality instead. I mean, they even kept the old VB6 error-handling model with On Error, Goto and Resume, even though exceptions are the intended (and more powerful) way to handle errors in .NET code ;-)Barrios
@Mike - I haven't really looked into the VBFixedStringAttribute class to determine if using it is a bad idea or not, but I do believe this question is a dupe of the one I linked to. I also think your answer is better than the ones found there, so +1 to you. (and of course I agree with you that using the old VB6 style of error handling is poor choice in a VB.NET app!)Kimmy
@Mike Why avoid that namespace? It is just a .Net assembly, like any other! It's part of VB and there's no reason not to use it. It's not going away. Now the Microsoft.VisualBasic.Compatibility assembly is one to avoid, though.Jurywoman
@Chris: True, Microsoft.VisualBasic.Compatibility is a lot worse, but the Microsoft.VisualBasic namespace can cause headaches. Part of the problem is that it's designed to work so similarly to VB6 that it gets confusing when it doesn't actually work the same way. For example, they even re-implemented most of the VB6 date-and-time functions (like DateSerial and friends), even though the .NET framework already has classes do all those things. Another example: it is confusing when half of the code uses the old-style ErrObject and half uses .NET exceptions, for example.Barrios
@Chris: This depends on the programmer of course, but at least in my experience VB.NET trying to mirror the VB6 runtime and encouraging the same style of programming just led to confusion for developers here, and it also encouraged them to not explore the rest of .NET or to actually learn how to write object-oriented code, because they were content with using all the "global" classes and functions VB.NET provided, which are really just wrappers around native .NET classes. YMMV, of course.Barrios
Related: How to declare a fixed-length string in VB.NET? (Only related because this has the special case of being a single character string.)Naga
B
10

That's known as a "fixed-length" string. There isn't an exact equivalent in VB.NET.

Edit: Well, OK, there's VBFixedStringAttribute, but I'm pretty sure that exists solely so that automated migration tools can more easily convert VB6 code to VB.NET for you, and it's not really the ".NET way" to do things. Also see the warnings in the article for details on why this still isn't exactly the same thing as a fixed-length string in VB6.

Generally, fixed-length strings are only used in VB6 if you are reading fixed-size records from a file or over the network (i.e. parsing headers in a protocol frame).

For example, you might have a file that contains a set of fixed-length records that all have the format (integer, 1-character-string, double), which you could represent in VB6 as a user-defined type:

Public Type Record
   anInteger As Integer
   aSingleCharacter As String * 1
   aDouble As Double
End Type

This way, VB6 code that reads from the file containing records in this format can read each fixed-sized record stored in the file, and in particular, it will only read 1 byte for aSingleCharacter. Without the * 1, VB6 would have no idea how many characters to read from the file, since a String can normally have any number of characters.

In VB.NET, you can do one of the following, depending on your needs:

  • If the length matters (i.e. you need to read exactly one byte from some data source, for example) consider using an array instead, such as

    Dim aSingleByteArray(1) As Byte

  • Alternatively, you could use one of the Stream classes. In particular, if you are reading data from a network socket or a file, consider using NetworkStream or FileStream, respectively. A Stream is meant for byte-for-byte access (i.e. raw binary access). StreamReader is a related class that simplifies reading data when it is text-based, so that might be good if you are reading a text file, for example. Otherwise (if dealing with binary data), stick with one of the Stream classes.

  • If the length doesn't matter, you could just use a "normal" String. That is to say:

    Dim aNormalString As String

Which answer is "correct" really depends on why it was declared that way in the original VB6 code.

Barrios answered 6/3, 2012 at 7:4 Comment(0)
N
2

Seeing as you're doing a VB6 migration, I'd definitely consider VBFixedStringAttribute as well as the other options listed by Mike Spross, but, in this case, because it is a single character, a Char may be an option in this case too.

As mentioned elsewhere VBFixedString is only acknowledged by the Get and Put VB I/O API. So the best solution (other than rewriting your code that references the "fixed length string") is to write your own equivalent of Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString. See this answer for more details.

Naga answered 6/3, 2012 at 9:24 Comment(2)
Char is certainly the closest equivalent.Braasch
@Derek: Whether Char is equivalent depends on the context. In VB6, strings are stored as Unicode, but if you are reading or writing a file, a String * 1 is treated as a single byte (8 bits), not as a character (16 bits), even if you put a Unicode character in the string. VB6 also automatically converts string parameters to ASCII when you call external functions (i.e. Windows API calls) declared with a String parameter. On that note, VB.NET's String data type (suggested in my answer) isn't technically equivalent either for the same reason. It depends on what the OP is doing with it.Barrios
C
2

The fixed length strings has been deprecated in VB.NET because there are several better options.

Since your fixed length string is just one character long, you can use the Char type in this case, as Mark suggested.

Dim strChar1 As Char
Cabezon answered 6/3, 2012 at 13:3 Comment(0)
B
0

VBFixedStringAttribute Class

<VBFixedString(1)> Dim strChar1 As String

Buber answered 12/12, 2019 at 15:20 Comment(2)
This question was answered 7 years ago. If you think that your answer is better compared to accepted answer then please add extra information or explanation.Gabbi
I have no more information about how to set variable to fix lenght string.Buber
M
0

ALthough this question was asked ages ago, VB.NET actually has a native fixed-length string -- <VbFixedArray(9)> Public fxdString As Char() 'declare 10-char fixed array. Doing this with scalars actually creates VB6-style Static Arrays.

Mornings answered 7/10, 2021 at 20:9 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Waver
For a documentation on VBFixedArray compare: learn.microsoft.com/en-us/dotnet/api/…Ait

© 2022 - 2024 — McMap. All rights reserved.