如何在 VB.NET 中声明和初始化一个多维数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14990561/
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
How to I declare and initialize a multidimensional array in VB.NET?
提问by oscilatingcretin
I want to do this:
我想做这个:
Dim Numbers As Integer()() = {{1}, {2}, {3}, {4, 5, 6, 7}}
The IDE's underlining 4, 5, 6, 7and saying Array initializer has 3 too many elements. What am I doing wrong?
IDE 的下划线4, 5, 6, 7和说Array initializer has 3 too many elements. 我究竟做错了什么?
回答by Oded
The following should work:
以下应该工作:
Dim Numbers As Integer()() = {({1}), ({2}), ({3}), ({4, 5, 6, 7})}
As documents in Arrays in Visual Basic:
作为Visual Basic 数组中的文档:
You can avoid an error when you supply nested array literals of different dimensions by enclosing the inner array literals in parentheses. The parentheses force the array literal expression to be evaluated, and the resulting values are used with the outer array literal
通过将内部数组文字括在括号中,您可以在提供不同维度的嵌套数组文字时避免错误。括号强制对数组字面量表达式求值,结果值与外部数组字面量一起使用

