Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Dim v As String() and Dim v() As String?

This may sound trivial, but what is the difference between

Dim v As String()

and

Dim v() As String

in VB.NET?

like image 823
Vincent Avatar asked Feb 10 '09 16:02

Vincent


4 Answers

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.

like image 180
Zach Scrivena Avatar answered Sep 21 '22 05:09

Zach Scrivena


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 51-element single-precision array

Then

 DIM A(50)    ' define 51-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 51-element single-precision array
 DIM B as String 
 DIM C(50) as String ' define 51-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.
like image 29
James Curran Avatar answered Sep 22 '22 05:09

James Curran


There is no difference.

Both Dim v As String() and Dim v() As String will create a string array

like image 30
Sean Avatar answered Sep 22 '22 05:09

Sean


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.

like image 38
Joacim Andersson Avatar answered Sep 19 '22 05:09

Joacim Andersson