相当于“Dim As String * 1"VB6 到 VB.NET [英] Equivalent of "Dim As String * 1" VB6 to VB.NET

查看:25
本文介绍了相当于“Dim As String * 1"VB6 到 VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Dim strChar1 As String * 1

Intellisense 不断告诉我语句结束.

解决方案

这就是所谓的固定长度"细绳.在 VB.NET 中没有精确的等价物.

<块引用>

编辑:好吧,有 VBFixedStringAttribute,但我很确定它的存在仅仅是为了让自动迁移工具可以更轻松地为您将 VB6 代码转换为 VB.NET,而它并不是真正的.NET方式"做事.另请参阅文章中的警告以详细了解为什么这仍然与 VB6 中的固定长度字符串不完全相同.

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

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

<前>公共类型记录anInteger 作为整数aSingleCharacter 作为字符串 * 1双倍双倍结束类型

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

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

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

    Dim aSingleByteArray(1) As Byte

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

  • 如果长度无关紧要,您可以使用普通"字符串.也就是说:

    Dim aNormalString As String

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

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.

解决方案

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.

这篇关于相当于“Dim As String * 1"VB6 到 VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆