I've been wondering for a long time what (if any) the difference is between the following:
Dim myString() as String
and
Dim myString as String()
I've been wondering for a long time what (if any) the difference is between the following:
Dim myString() as String
and
Dim myString as String()
There is no difference. Both initialize a variable to an array of String equal to Nothing. You'll find that in VB there can be more than one way to do the same thing.
Nonetheless, Microsoft's VB Coding Conventions has this to say regarding arrays:
Put the array designator on the type, not on the variable. For example, use the following syntax:
Dim letters4 As String() = {"a", "b", "c"}Do not use the following syntax:
Dim letters3() As String
There are some differences between the two syntaxes, which I will try to summarize. The first is the de facto VB syntax for declaring an array with a size but that argument is optional.
'Declare a single-dimension array of 5 values
Dim numbers(4) As Integer
'Declare a multi-dimensional array
Dim matrix(5, 5) As Double
You can't use the second syntax with a size, however:
Dim numbers as Integer(4)
'Compiler error: Array bounds cannot appear in type specifiers
But you can with the new operator and an initializer!
'Also an empty array with capacity for 5 values
Dim numbers as Integer() = new Integer(4) { }
Which brings us to the second syntax: this is used when we want to declare and populate an array with initial values (i.e. an array literal)
Dim values As Double() = {1, 2, 3, 4, 5, 6}
In your second case, you've simply omitted the array literal and therefore results in an expression equivalent to the first.
See Arrays in Visual Basic in MSDN.