views:

2398

answers:

6

This may sound trivial, but what is the difference between Dim v As String() and Dim v() As String in VB.NET?

+1  A: 

There is no difference.

Kibbee
v.Length will not be available if you declare v as String(), but will be available if you declare it v() As String
Vincent
Seems to be available on my compiler
Kibbee
+1  A: 

It's mostly semantics. The first reads as create variable "v" of type string array and the 2nd reads as create array "v" of type string. Either way the result is the same array of strings.

Joel Coehoorn
+8  A: 

No difference. From the VB.NET Language Specification on Arrays:

Array types are specified by adding a modifier to an existing type name. The modifier consists of a left parenthesis, a set of zero or more commas, and a right parenthesis.

...

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. In that case, the array element type is the type given in the declaration, and the array dimensions are determined by the variable name modifier. 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.

Zach Scrivena
+2  A: 

Originally, in Basic, you had to define arrays, but not variables. And the types of variables were defined by a suffix character: A$ was a string, while A% was an integer and A# was double precision. (and all three were distinct and could be used at the same time) (For single-precision, you could use A!, but that was the default if you just used A)

Eventually, programmers came to realize that those were incredibly bad design choices.

To rectify this, Microsoft added "Option Explicit" which required you to predefine every variable. To lessen the effect on the language, they hijack the "DIM" command, which was used to define arrays, to define scalar variables as well.

So originally:

 DIM A(50)    ' define 50-element single-precision array

Then

 DIM A(50)    ' define 50-element single-precision array
 DIM A$       ' define a string

Then to get rid of the suffixes, they added the "As {type} syntax"

 DIM A(50)    ' define 50-element single-precision array
 DIM B as String 
 DIM C(50) as String ' define 50-element string array.

Then they made array size variable.

 DIM A()    ' define single-precision array
 DIM B as String 
 DIM C() as String ' define string array.

This left a conflict in definition style, so they allowed both:

 DIM A()    ' define single-precision array
 DIM B as String 
 DIM C() as String ' define string array.
 DIM D as String() ' define string array.
James Curran
A: 

There is no difference in the meaning of the two.

If you like to declare several variables in one dim statement, the second form provides more flexibility: dim v(),v2 as string allows you to declare array types and non array types in the same statement.

Renze de Waal
A: 

Traditionally, in Basic, you would put the parenthesis after the variable name. In VB.Net it is allowed to put them after the type instead if you wish. The result is the same so there is no difference using either syntax. The reason for this addition though is because how you can constuct an array. Consider the following code:

  Public Sub MethodThatExpectsAnArray(ByVal arr() As String)
    '...
  End Sub

  Public Sub Main()
    Me.MethodThatExpectsAnArray(New String() {"Hello", "World"})
  End Sub

In the call I construct the array "on the fly" without any assignment except directly to the method argument. Since there are no variable here I must set the paranthesis after the type. To allow this syntax Microsoft had the choice to either change how you traditionally declare arrays in Basic or allow for both syntaxes. They of course opted for the latter.

Joacim Andersson