Dim v As String()和Dim v()As String有什么区别?

时间:2022-04-03 13:58:41

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

这可能听起来微不足道,但是在VB.NET中Dim v As String()和Dim v()As String之间有什么区别?

7 个解决方案

#1


10  

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

没有不同。从阵列上的VB.NET语言规范:

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.

通过在变量名称上放置数组类型修饰符或数组初始化修饰符,也可以将变量声明为数组类型。在这种情况下,数组元素类型是声明中给出的类型,数组维度由变量名称修饰符确定。为清楚起见,在同一声明中的变量名和类型名上都有一个数组类型修饰符是无效的。

#2


8  

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)

最初,在Basic中,您必须定义数组,而不是变量。变量的类型由后缀字符定义:A $是一个字符串,而A%是一个整数,A#是双精度。 (并且所有三个都是截然不同的,可以同时使用)(对于单精度,你可以使用A !,但如果你只使用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.

为了解决这个问题,微软增加了“Option Explicit”,它要求你预定义每个变量。为了减轻对语言的影响,他们劫持了用于定义数组的“DIM”命令,以定义标量变量。

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"

然后为了摆脱后缀,他们添加了“As {type}语法”

 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.

#3


3  

There is no difference.

没有区别。

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

Dim v As String()和Dim v()As String都将创建一个字符串数组

#4


2  

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:

传统上,在Basic中,您可以将括号放在变量名后面。在VB.Net中,如果您愿意,可以将它们放在类型之后。结果是相同的,因此使用任一语法都没有区别。这个添加的原因是因为你可以如何构建一个数组。请考虑以下代码:

  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.

在调用中,我构造了“动态”数组,没有任何赋值,除了直接到方法参数。由于这里没有变量,我必须在类型后面设置paranthesis。为了允许这种语法,Microsoft可以选择更改传统方式在Basic中声明数组的方式或允许两种语法。他们当然选择了后者。

#5


1  

There is no difference.

没有区别。

#6


1  

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.

它主要是语义学。第一个读取为创建变量“v”的类型字符串数组,第二个读取为创建数组字符串“v”。无论哪种方式,结果都是相同的字符串数组。

#7


0  

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.

如果你想在一个dim语句中声明几个变量,第二个表单提供了更大的灵活性:dim v(),v2 as string允许你在同一个语句中声明数组类型和非数组类型。

#1


10  

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

没有不同。从阵列上的VB.NET语言规范:

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.

通过在变量名称上放置数组类型修饰符或数组初始化修饰符,也可以将变量声明为数组类型。在这种情况下,数组元素类型是声明中给出的类型,数组维度由变量名称修饰符确定。为清楚起见,在同一声明中的变量名和类型名上都有一个数组类型修饰符是无效的。

#2


8  

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)

最初,在Basic中,您必须定义数组,而不是变量。变量的类型由后缀字符定义:A $是一个字符串,而A%是一个整数,A#是双精度。 (并且所有三个都是截然不同的,可以同时使用)(对于单精度,你可以使用A !,但如果你只使用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.

为了解决这个问题,微软增加了“Option Explicit”,它要求你预定义每个变量。为了减轻对语言的影响,他们劫持了用于定义数组的“DIM”命令,以定义标量变量。

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"

然后为了摆脱后缀,他们添加了“As {type}语法”

 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.

#3


3  

There is no difference.

没有区别。

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

Dim v As String()和Dim v()As String都将创建一个字符串数组

#4


2  

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:

传统上,在Basic中,您可以将括号放在变量名后面。在VB.Net中,如果您愿意,可以将它们放在类型之后。结果是相同的,因此使用任一语法都没有区别。这个添加的原因是因为你可以如何构建一个数组。请考虑以下代码:

  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.

在调用中,我构造了“动态”数组,没有任何赋值,除了直接到方法参数。由于这里没有变量,我必须在类型后面设置paranthesis。为了允许这种语法,Microsoft可以选择更改传统方式在Basic中声明数组的方式或允许两种语法。他们当然选择了后者。

#5


1  

There is no difference.

没有区别。

#6


1  

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.

它主要是语义学。第一个读取为创建变量“v”的类型字符串数组,第二个读取为创建数组字符串“v”。无论哪种方式,结果都是相同的字符串数组。

#7


0  

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.

如果你想在一个dim语句中声明几个变量,第二个表单提供了更大的灵活性:dim v(),v2 as string允许你在同一个语句中声明数组类型和非数组类型。