1

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()
  • 1
    Honestly, ***nothing***... Each way is still initializing/allocating the variable as a `String Array`... – Trevor Jan 19 '16 at 16:37
  • 1
    As already noted nothing. I use Dim varName As String() since I move between C# and vb.net where in C# we have string[] varName. So for you it's personal preference – Karen Payne Jan 19 '16 at 16:41
  • The second way is the only way you could do it in older versions of VB but with the introduction of generics, some things cant be dimensioned in that way anymore so they created the ability to do it the first way. But, they left the second way for backwards compatibility. I suggest using the first way always in case they decide to remove the second way. (This is my guess) – Steve Jan 19 '16 at 16:44
  • 1
    @Steve ***what's the difference***? – Trevor Jan 19 '16 at 16:46
  • @Codexer They are the same. Maybe readability. BTW, you can do that with generics too but it looks confusing. `Dim s As ObjectModel.Collection(Of String)()` compared to `Dim s() As ObjectModel.Collection(Of String)` – Steve Jan 19 '16 at 16:49
  • @Steve thanks for clarification, I agree for readability... Why not just `Dim s As ObjectModel.Collection(Of String())`? – Trevor Jan 19 '16 at 16:52
  • 1
    @Codexer Why use an array at all. :-) – Steve Jan 19 '16 at 16:55

1 Answers1

2

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.

Community
  • 1
  • 1
ardila
  • 1,277
  • 1
  • 13
  • 24
  • 1
    You could quote the MSDN [Visual Basic Coding Conventions](https://msdn.microsoft.com/en-us/library/h63fsef3.aspx) which state "Put the array designator on the type, not on the variable." – Andrew Morton Jan 19 '16 at 19:00
  • Cheers, I'll edit to include the reference you've provided. – ardila Jan 19 '16 at 21:15