myString()或As String() [英] myString() or As String()

查看:51
本文介绍了myString()或As String()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间以来,我一直想知道以下各项之间的区别(如果有):

I've been wondering for a long time what (if any) the difference is between the following:

Dim myString() as String

Dim myString as String()

推荐答案

没有区别.两者都将变量初始化为等于 Nothing String 数组.您会发现,在VB中,可以使用多种方法来完成同一件事.尽管如此, Microsoft的VB编码约定还是要对数组说:/p>

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"}

请勿使用以下语法:

Dim letters3() As String

两种语法之间有一些区别,我将尝试总结一下.第一种是事实上的VB语法,用于声明具有大小的数组,但该参数是可选的.

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

但是您可以使用 new 运算符和一个初始化程序!

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) { }

这带给我们第二种语法:当我们要声明 并使用初始值(即 array文字)填充数组时使用此语法

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}

在第二种情况下,您仅省略了 array文字,因此得出的表达式与第一种等效.

In your second case, you've simply omitted the array literal and therefore results in an expression equivalent to the first.

请参见 Visual Basic中的数组 在MSDN中.

See Arrays in Visual Basic in MSDN.

这篇关于myString()或As String()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆