vba 等效于“Dim As String * 1” VB6 到 VB.NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9578990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:27:56  来源:igfitidea点击:

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

vb.netstringvbavb6vb6-migration

提问by Logan B. Lehman

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

我有一些VB6代码需要迁移到VB.NET,想咨询一下这行代码,看看有没有.NET实现的方法

Dim strChar1 As String * 1

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

Intellisense 不断告诉我,预计会结束语句。

回答by Mike Spross

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

这被称为“固定长度”字符串。在 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.

编辑:嗯,好的,有VBFixedStringAttribute,但我很确定它的存在只是为了让自动迁移工具可以更轻松地为您将 VB6 代码转换为 VB.NET,这并不是真正的“.NET 方式”。另请参阅文章中的警告以了解有关为什么这仍然与 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).

通常,如果您从文件或通过网络读取固定大小的记录(即解析协议帧中的标头),则固定长度字符串仅在 VB6 中使用。

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:

例如,您可能有一个包含一组固定长度记录的文件,这些记录的格式都为(integer, 1-character-string, double),您可以在 VB6 中将其表示为用户定义的类型:

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 Stringcan normally have any number of characters.

这样,从包含这种格式记录的文件中读取的 VB6 代码可以读取文件中存储的每个固定大小的记录,特别是对于aSingleCharacter. 如果没有* 1,VB6 将不知道从文件中读取多少个字符,因为 aString通常可以有任意数量的字符。

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

在 VB.NET 中,您可以根据需要执行以下操作之一:

  • 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 Streamclasses. In particular, if you are reading data from a network socket or a file, consider using NetworkStreamor FileStream, respectively. A Streamis meant for byte-for-byte access (i.e. raw binary access). StreamReaderis 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 Streamclasses.

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

    Dim aNormalString As String

  • 如果长度很重要(例如,您需要从某个数据源读取一个字节),请考虑使用数组,例如

    Dim aSingleByteArray(1) As Byte

  • 或者,您可以使用Stream类之一。特别是,如果您从网络套接字或文件读取数据,请考虑分别使用NetworkStreamFileStream。甲是为字节对字节访问(即原始二进制访问)。StreamReader是一个相关的类,当它基于文本时,它简化了读取数据,因此,例如,如果您正在读取文本文件,这可能会很好。否则(如果处理二进制数据),坚持使用Stream类之一。

  • 如果长度无关紧要,您可以只使用 "normal" String。也就是说:

    Dim aNormalString As String

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

哪个答案是“正确的”实际上取决于为什么在原始 VB6 代码中以这种方式声明它。

回答by Mark Hurd

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

看到您正在执行 VB6 迁移,我肯定会考虑VBFixedStringAttribute以及 Mike Spross 列出的其他选项,但是,在这种情况下,因为它是单个字符,因此Char在这种情况下a也可能是一个选项。

As mentioned elsewhere VBFixedStringis only acknowledged by the Getand PutVB 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 answerfor more details.

正如其他地方所提到的VBFixedString,只有GetPutVB I/O API才承认。因此,最好的解决方案(除了重写引用“固定长度字符串”的代码之外)是编写自己的Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString. 有关更多详细信息,请参阅此答案

回答by Pradeep Kumar

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

固定长度字符串在 VB.NET 中已被弃用,因为有几个更好的选择。

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

由于您的固定长度字符串只有一个字符长,因此您可以在这种情况下使用 Char 类型,正如 Mark 建议的那样。

Dim strChar1 As Char

回答by profess79

VBFixedStringAttribute Class

VBFixedStringAttribute 类

<VBFixedString(1)> Dim strChar1 As String

<VBFixedString(1)> Dim strChar1 As String