在VB.NET中声明数组的不同方法 [英] Different ways of declaring arrays in VB.NET

查看:256
本文介绍了在VB.NET中声明数组的不同方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB.NET中,以下声明数组的方式之间有什么区别吗?

In VB.NET, is there any difference between the following ways of declaring arrays?

- Dim cargoWeights(10) as Double

- cargoWeights = New Double(10) {}

'这是两个独立的陈述。

' These are two independent statements. They are not supposed to execute one after the other.

据我所知,第一个只是声明一个数组变量,该变量在某个数组之前保持值为 Nothing。对象分配给它。换句话说,它尚未初始化。

As far as I know, the first one just declares an array variable that holds the value 'Nothing' until some array object is assigned to it. In other words, it is not initialized yet.

但是第二条语句呢? =符号是否表示该变量已被初始化并且不会包含 Nothing?它会指向11个默认Double值(0.0)的一维数组吗?

But what about the second statement? Does the "=" sign means the variable is already being initialized and won't hold 'Nothing'? Is it going to point to an one-dimensional array of eleven default Double values (0.0)?

编辑:

根据MSDN网站:


下面的示例声明一个数组变量,该变量最初并不指向任何数组。

The following example declares an array variable that does not initially point to any array.

Dim twoDimStrings(,)作为字符串

Dim twoDimStrings( , ) As String

(...)变量twoDimStrings的值为Nothing。

(...) the variable twoDimStrings has the value Nothing.

来源: http://msdn.microsoft.com/zh-cn/library/18e9wyy0(v = vs.80).aspx

推荐答案

Dim cargoWeights(10)都设为Double cargoWeights =新Double(10){} 实际上将初始化一个double数组,并将每个项目设置为默认类型值,在这种情况下为0.0。 (或者如果 String 数据类型什么也没有)

Both Dim cargoWeights(10) as Double and cargoWeights = New Double(10) {} will actually initialize an array of doubles with each items set to default type value, which in this case, 0.0. (Or Nothing if String data type)

两种语法的区别在于,第二种语法可以将数组中每个项目的值初始化为不同于默认值的值,例如:

The difference between the two syntax is that, the 2nd one you can init the value of each items in the array to different from default value, like:

cargoWeights = New Double(10) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}

要声明未初始化的数组,请使用 Dim cargoWeights()为Double cargoWeights = New Double(){}

To declare uninitialized array, use Dim cargoWeights() As Double or cargoWeights = New Double() {}.

这篇关于在VB.NET中声明数组的不同方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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