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
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.
They're reported as "var1() as byte" and "var2() as byte"
even though the first was declared with the alternate syntax.
There's no difference.
Quotes from the spec (2003 spec, but same in the 2010 spec as can be downloaded here):
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With