You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET中如何初始化作为类成员的字节交错数组?

解决VB.NET交错数组初始化的BC30201错误

错误原因分析

你的代码存在两个核心问题导致BC30201编译错误:

  • 交错数组初始化语法错误:VB.NET中New Byte(2)()表示创建包含3个元素的交错数组,但你仅提供了2个子数组;同时数组初始化列表的最后一个元素后不能保留多余逗号。
  • 变量未指定类型:DecoderInstance和测试方法参数message_number未声明类型,不符合VB语法规范,也会引发编译异常。

修正后的测试类代码

Imports System.Text
Imports Microsoft.VisualStudio.TestTools.UnitTesting

Namespace TestDecoder.Tests
    <TestClass>
    Public Class DecoderTests
        ' 修正交错数组初始化:移除外层数组长度声明(或改为New Byte(1)()对应2个元素),删除最后一个元素后的逗号
        Private ReadOnly TestMsgs As Byte()() = {
            New Byte() {&HA1, &HB2, &HC3}, 'Test array should Pass
            New Byte() {&HA2, &HB3}        'Test array should Fail
        }
        ' 明确声明变量类型
        Private DecoderInstance As Decoder

        <DataTestMethod>
        <DataRow(0)>
        <DataRow(1)>
        ' 明确参数类型
        Public Sub ParseTestData(ByVal message_number As Integer)
            Dim result As Boolean
            DecoderInstance = New Decoder()
            result = DecoderInstance.parse(TestMsgs(message_number)(0))
            Assert.IsTrue(result, "Failed the dummy test")
        End Sub
    End Class
End Namespace

补充说明

  1. 交错数组初始化时,若不指定外层数组长度,VB会自动根据提供的子数组数量推断长度,这种写法更简洁且不易出错。
  2. 所有变量和方法参数明确声明类型,既符合VB语法要求,也能提升代码可读性与编译效率。

内容的提问来源于stack exchange,提问作者harry courtice

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 20:30:27