将 VB6 模块转换为 VB.NET

发布于 2024-12-19 09:24:35 字数 378 浏览 3 评论 0原文

我几乎已经完成了从 VB6 到 VB.NET 的模块转换,但我在使用以下 2 个引号时遇到了问题,并且想知道是否有任何方法可以解决此问题:

Structure AUDINPUTARRAY
    bytes(5000) As Byte
End Structure

我正在尝试将该字节行更改为: Dim字节(5000)作为字节 但它不允许我定义结构的大小。


这是第二个:

Private i As Integer, j As Integer, msg As String * 200, hWaveIn As integer

我不知道如何转换:msg As String * 200

I'm almost done converting a module from VB6 to VB.NET, but I'm having trouble with the following 2 quotes and am wondering if there's any way to go about this:

Structure AUDINPUTARRAY
    bytes(5000) As Byte
End Structure

I'm trying to change that bytes line to: Dim bytes(5000) as Byte
but it's not letting me define the size in a structure.


Here's the second one:

Private i As Integer, j As Integer, msg As String * 200, hWaveIn As integer

I haven't a clue on how to convert: msg As String * 200

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自我难过 2024-12-26 09:24:35

您无法在 VB.Net 中声明初始大小,您可以稍后在构造函数中或任何需要的地方使用 Redim 语句设置其大小。

Structure AUDINPUTARRAY
    Public bytes() As Byte
    Public Sub New(ByVal size As Integer)
        ReDim bytes(size) ' set size=5000

    End Sub


End Structure

在 Visual Basic .NET 中,您无法将字符串声明为具有固定长度,除非您在宣言。前面示例中的代码会导致错误。

您声明一个没有长度的字符串。当您的代码为字符串赋值时,该值的长度决定了字符串的长度
请参阅 http://msdn.microsoft.com/ en-us/library/f47b0zy4%28v=vs.71%29.aspx
。所以你的声明将变成

    Private i As Integer, j As Integer, hWaveIn As Integer
    <VBFixedString(200)> Private msg As String

you cannot declare an initial size in VB.Net , you can set its size later using Redim statement in constructor or wherever needed

Structure AUDINPUTARRAY
    Public bytes() As Byte
    Public Sub New(ByVal size As Integer)
        ReDim bytes(size) ' set size=5000

    End Sub


End Structure

In Visual Basic .NET, you cannot declare a string to have a fixed length unless you use the VBFixedStringAttribute Class attribute in the declaration. The code in the preceding example causes an error.

You declare a string without a length. When your code assigns a value to the string, the length of the value determines the length of the string
see http://msdn.microsoft.com/en-us/library/f47b0zy4%28v=vs.71%29.aspx
. so your declarration will become

    Private i As Integer, j As Integer, hWaveIn As Integer
    <VBFixedString(200)> Private msg As String
裸钻 2024-12-26 09:24:35

您可以通过属性来做到这一点

Public Structure <StructLayout(LayoutKind.Sequential)> AUDINPUTARRAY
   Public <MarshalAs(UnmanagedType.ByValArray, SizeConst := 5000)> 
     Bytes() As Byte
End Structure

You can do this via attributes

Public Structure <StructLayout(LayoutKind.Sequential)> AUDINPUTARRAY
   Public <MarshalAs(UnmanagedType.ByValArray, SizeConst := 5000)> 
     Bytes() As Byte
End Structure
愿得七秒忆 2024-12-26 09:24:35

我建议,在将代码从 VB6 重构为 .net 时,您再考虑一下是否要模拟固定长度的 msg As String * 200。如果您指望固定长度的字符串,以便可以从末尾截去字符,并且仍然有 200 个字符的记录,那么这就是依赖于函数副作用的混乱代码。

当我们从 VB6 转换(仍在进行中)时,如果我们将字符串显式设置为 200 字节的空格块,则代码的意图会更清晰。也许可以通过声明:

String msg = String(' ', 200)

(如果这在 VB.net 和 C# 中都有效)。

I would suggest that, while refactoring your code from VB6 to .net, that you take another look at whether you even want to emulate the fixed-length msg As String * 200. If you were counting on the fixed-length string so that you could chop characters off of the end, and still have a 200-character record, that's messy code that depends on a function's side effects.

When we converted from VB6 (a still-ongoing process), it made the intent of the code clearer if we explicitly set the string to a 200-byte block of spaces. Perhaps by declaring:

String msg = String(' ', 200)

(if that's valid in VB.net as well as C#).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文