When declaring a byte array, what is the difference between the following? Is there one, or are these just two different ways of going about the same thing?
声明一个字节数组时,以下是什么区别?有没有一个,或者这两种方式只是两种不同的方式?
Dim var1 As Byte()
Dim var2() As Byte
2 个解决方案
#1
4
There's no difference.
没有区别。
Quotes from the spec (2003 spec, but same in the 2010 spec as can be downloaded here):
规范引用(2003规范,但2010年规范中的相同内容可在此处下载):
Array types are specified by adding a modifier to an existing type name.
通过向现有类型名称添加修饰符来指定数组类型。
A variable may also be declared to be of an array type by putting an array type modifier or an array initialization modifier on the variable name.
通过在变量名称上放置数组类型修饰符或数组初始化修饰符,也可以将变量声明为数组类型。
For clarity, it is not valid to have an array type modifier on both a variable name and a type name in the same declaration.
为清楚起见,在同一声明中的变量名和类型名上都有一个数组类型修饰符是无效的。
And below is the sample from the spec that shows all the options:
以下是规范中显示所有选项的示例:
Module Test
Sub Main()
Dim a1() As Integer ' Declares 1-dimensional array of integers.
Dim a2(,) As Integer ' Declares 2-dimensional array of integers.
Dim a3(,,) As Integer ' Declares 3-dimensional array of integers.
Dim a4 As Integer() ' Declares 1-dimensional array of integers.
Dim a5 As Integer(,) ' Declares 2-dimensional array of integers.
Dim a6 As Integer(,,) ' Declares 3-dimensional array of integers.
' Declare 1-dimensional array of 2-dimensional arrays of integers
Dim a7()(,) As Integer
' Declare 2-dimensional array of 1-dimensional arrays of integers.
Dim a8(,)() As Integer
Dim a9() As Integer() ' Not allowed.
End Sub
End Module
And as can be seen in the comments, a1 and a4 does the same thing.
从评论中可以看出,a1和a4做同样的事情。
#2
4
They're the same thing. You can verify by looking at the compiled code in reflector, or by writing that code in the IDE, then hovering your mouse over each.
他们是一回事。您可以通过查看反射器中的已编译代码或通过在IDE中编写该代码来验证,然后将鼠标悬停在每个代码上。
They're reported as "var1() as byte" and "var2() as byte"
它们被报告为“var1()as byte”和“var2()as byte”
even though the first was declared with the alternate syntax.
即使第一个是用替代语法声明的。
#1
4
There's no difference.
没有区别。
Quotes from the spec (2003 spec, but same in the 2010 spec as can be downloaded here):
规范引用(2003规范,但2010年规范中的相同内容可在此处下载):
Array types are specified by adding a modifier to an existing type name.
通过向现有类型名称添加修饰符来指定数组类型。
A variable may also be declared to be of an array type by putting an array type modifier or an array initialization modifier on the variable name.
通过在变量名称上放置数组类型修饰符或数组初始化修饰符,也可以将变量声明为数组类型。
For clarity, it is not valid to have an array type modifier on both a variable name and a type name in the same declaration.
为清楚起见,在同一声明中的变量名和类型名上都有一个数组类型修饰符是无效的。
And below is the sample from the spec that shows all the options:
以下是规范中显示所有选项的示例:
Module Test
Sub Main()
Dim a1() As Integer ' Declares 1-dimensional array of integers.
Dim a2(,) As Integer ' Declares 2-dimensional array of integers.
Dim a3(,,) As Integer ' Declares 3-dimensional array of integers.
Dim a4 As Integer() ' Declares 1-dimensional array of integers.
Dim a5 As Integer(,) ' Declares 2-dimensional array of integers.
Dim a6 As Integer(,,) ' Declares 3-dimensional array of integers.
' Declare 1-dimensional array of 2-dimensional arrays of integers
Dim a7()(,) As Integer
' Declare 2-dimensional array of 1-dimensional arrays of integers.
Dim a8(,)() As Integer
Dim a9() As Integer() ' Not allowed.
End Sub
End Module
And as can be seen in the comments, a1 and a4 does the same thing.
从评论中可以看出,a1和a4做同样的事情。
#2
4
They're the same thing. You can verify by looking at the compiled code in reflector, or by writing that code in the IDE, then hovering your mouse over each.
他们是一回事。您可以通过查看反射器中的已编译代码或通过在IDE中编写该代码来验证,然后将鼠标悬停在每个代码上。
They're reported as "var1() as byte" and "var2() as byte"
它们被报告为“var1()as byte”和“var2()as byte”
even though the first was declared with the alternate syntax.
即使第一个是用替代语法声明的。