S
Stephany Young
Time for a revisit on this one Zytan. I had a bit of time this afternoon and
I was getting a tad curious.
This page here http://msdn2.microsoft.com/en-us/library/7ee5a7s1(VS.80).aspx
is the key to understanding why these forms are both valid.
Dim emptyArray() as Byte = New Byte() {}
Dim emptyArray as Byte() = New Byte() {}
It won't tell you in so many words, but the content of that page and some of
it's links show the following.
The page states that to specify that a variable is an array, you follow its
variablename immediately with parentheses (as in the first form).
Other information shows that Byte and Byte() are distinct data types.
Byte is a value type.
Byte() is a reference type derived from System.Array.
If you attempt to code:
Dim emptyArray() as Byte() = New Byte() {}
you get a compiler error that reads:
Array modifiers cannot be specified on both a variable and its type.
More specifically, the compiler error code is BC31087 which has the longer
description of:
Array modifiers are present on both the variable and its type, indicating
an array of arrays.
and the recommended fix is to remove the array modifier from the type
specifier. This is also what the intellisense error correction option
recommend for this condition.
This means that the form recommended by both the documentation and the
compiler is:
Dim emptyArray() as Byte = New Byte() {}
If you code:
Dim emptyArray as Byte() = New Byte() {}
then because the variable name is not followed by () and because Byte() is a
distinct type, it follows that declaration is quite legal.
The documentation further recommends that a zero-length array be declared
as:
Dim emptyArray(-1) As Byte
If you attempt to code:
Dim emptyArray As Byte(-1)
you get compiler error code BC31087: Array bounds cannot appear in type
specifiers.
This reinforces that the recommendations we came across earlier.
As to why we appeared to not know these things off the top off our heads, we
probably did, but because using these 'features' has become second nature,
the whys and wherefores are probably in an archived part of memory and not
readily accessible.
I was getting a tad curious.
This page here http://msdn2.microsoft.com/en-us/library/7ee5a7s1(VS.80).aspx
is the key to understanding why these forms are both valid.
Dim emptyArray() as Byte = New Byte() {}
Dim emptyArray as Byte() = New Byte() {}
It won't tell you in so many words, but the content of that page and some of
it's links show the following.
The page states that to specify that a variable is an array, you follow its
variablename immediately with parentheses (as in the first form).
Other information shows that Byte and Byte() are distinct data types.
Byte is a value type.
Byte() is a reference type derived from System.Array.
If you attempt to code:
Dim emptyArray() as Byte() = New Byte() {}
you get a compiler error that reads:
Array modifiers cannot be specified on both a variable and its type.
More specifically, the compiler error code is BC31087 which has the longer
description of:
Array modifiers are present on both the variable and its type, indicating
an array of arrays.
and the recommended fix is to remove the array modifier from the type
specifier. This is also what the intellisense error correction option
recommend for this condition.
This means that the form recommended by both the documentation and the
compiler is:
Dim emptyArray() as Byte = New Byte() {}
If you code:
Dim emptyArray as Byte() = New Byte() {}
then because the variable name is not followed by () and because Byte() is a
distinct type, it follows that declaration is quite legal.
The documentation further recommends that a zero-length array be declared
as:
Dim emptyArray(-1) As Byte
If you attempt to code:
Dim emptyArray As Byte(-1)
you get compiler error code BC31087: Array bounds cannot appear in type
specifiers.
This reinforces that the recommendations we came across earlier.
As to why we appeared to not know these things off the top off our heads, we
probably did, but because using these 'features' has become second nature,
the whys and wherefores are probably in an archived part of memory and not
readily accessible.