在VB.NET中声明和初始化字符串数组

时间:2022-05-26 21:15:08

I was trying to return an array of strings from a function and got surprised by an error.

我试图返回一个函数的字符串数组,并被一个错误所震惊。

I would have expected this to work, but it produces an error:

我本以为这行得通,但它产生了一个错误:

Public Function TestError() As String()
    Return {"foo", "bar"}
End Function

This works:

如此:

Public Function TestOK() As String()
    Dim ar As String() = {"foo", "bar"}
    Return ar
End Function

As does:

一样:

Public Function TestOK() As String()
    Return New String() {"foo", "bar"}
End Function

I guess I'm unclear on the meaning of the {}'s - is there a way to implicitly return a string array without explicitly creating and initializing it?

我猜我不清楚{}的含义——是否有一种方法可以在不显式创建和初始化字符串数组的情况下隐式返回字符串数组?

3 个解决方案

#1


30  

Array initializer support for type inference were changed in Visual Basic 10 vs Visual Basic 9.

在Visual Basic 10和Visual Basic 9中,数组初始化器对类型推断的支持被更改。

In previous version of VB it was required to put empty parens to signify an array. Also, it would define the array as object array unless otherwise was stated:

在VB的前一个版本中,需要使用空的括号来表示数组。此外,它将把数组定义为对象数组,除非另有说明:

' Integer array
Dim i as Integer() = {1, 2, 3, 4} 

' Object array
Dim o() = {1, 2, 3} 

Check more info:

检查更多的信息:

Visual Basic 2010 Breaking Changes

Visual Basic 2010打破变化

Collection and Array Initializers in Visual Basic 2010

Visual Basic 2010中的集合和数组初始化器

#2


11  

Public Function TestError() As String()
     Return {"foo", "bar"}
End Function

Works fine for me and should work for you, but you may need allow using implicit declarations in your project. I believe this is turning off Options strict in the Compile section of the program settings.

对我来说很好,应该也适合您,但是您可能需要允许在项目中使用隐式声明。我认为这是在程序设置的编译部分严格地关闭选项。

Since you are using VS 2008 (VB.NET 9.0) you have to declare create the new instance

因为你正在使用VS 2008 (VB)。您必须声明创建新实例

New String() {"foo", "Bar"}

新的字符串(){“foo”、“酒吧”}

#3


6  

I believe you need to specify "Option Infer On" for this to work.

我认为您需要指定“选项推断”以使其工作。

Option Infer allows the compiler to make a guess at what is being represented by your code, thus it will guess that {"stuff"} is an array of strings. With "Option Infer Off", {"stuff"} won't have any type assigned to it, ever, and so it will always fail, without a type specifier.

选项推断允许编译器猜测您的代码表示了什么,因此它会猜测{"stuff"}是字符串数组。使用“选项推断”,{“stuff”}不会有任何类型分配给它,因此它总是会失败,没有类型说明符。

Option Infer is, I think On by default in new projects, but Off by default when you migrate from earlier frameworks up to 3.5.

我认为,在新项目中,选项推断是默认的,但是当您从早期框架迁移到3.5时,默认是默认的。

Opinion incoming:

意见输入:

Also, you mention that you've got "Option Explicit Off". Please don't do this.

另外,你还提到了你有“明确的选项”。请不要这样做。

Setting "Option Explicit Off" means that you don't ever have to declare variables. This means that the following code will silently and invisibly create the variable "Y":

设置“选项显式关闭”意味着您不必声明变量。这意味着下面的代码将无声地、不可见地创建变量“Y”:

Dim X as Integer
Y = 3

This is horrible, mad, and wrong. It creates variables when you make typos. I keep hoping that they'll remove it from the language.

这是可怕的、疯狂的、错误的。它会在输入错误时创建变量。我一直希望他们能把它从语言中删除。

#1


30  

Array initializer support for type inference were changed in Visual Basic 10 vs Visual Basic 9.

在Visual Basic 10和Visual Basic 9中,数组初始化器对类型推断的支持被更改。

In previous version of VB it was required to put empty parens to signify an array. Also, it would define the array as object array unless otherwise was stated:

在VB的前一个版本中,需要使用空的括号来表示数组。此外,它将把数组定义为对象数组,除非另有说明:

' Integer array
Dim i as Integer() = {1, 2, 3, 4} 

' Object array
Dim o() = {1, 2, 3} 

Check more info:

检查更多的信息:

Visual Basic 2010 Breaking Changes

Visual Basic 2010打破变化

Collection and Array Initializers in Visual Basic 2010

Visual Basic 2010中的集合和数组初始化器

#2


11  

Public Function TestError() As String()
     Return {"foo", "bar"}
End Function

Works fine for me and should work for you, but you may need allow using implicit declarations in your project. I believe this is turning off Options strict in the Compile section of the program settings.

对我来说很好,应该也适合您,但是您可能需要允许在项目中使用隐式声明。我认为这是在程序设置的编译部分严格地关闭选项。

Since you are using VS 2008 (VB.NET 9.0) you have to declare create the new instance

因为你正在使用VS 2008 (VB)。您必须声明创建新实例

New String() {"foo", "Bar"}

新的字符串(){“foo”、“酒吧”}

#3


6  

I believe you need to specify "Option Infer On" for this to work.

我认为您需要指定“选项推断”以使其工作。

Option Infer allows the compiler to make a guess at what is being represented by your code, thus it will guess that {"stuff"} is an array of strings. With "Option Infer Off", {"stuff"} won't have any type assigned to it, ever, and so it will always fail, without a type specifier.

选项推断允许编译器猜测您的代码表示了什么,因此它会猜测{"stuff"}是字符串数组。使用“选项推断”,{“stuff”}不会有任何类型分配给它,因此它总是会失败,没有类型说明符。

Option Infer is, I think On by default in new projects, but Off by default when you migrate from earlier frameworks up to 3.5.

我认为,在新项目中,选项推断是默认的,但是当您从早期框架迁移到3.5时,默认是默认的。

Opinion incoming:

意见输入:

Also, you mention that you've got "Option Explicit Off". Please don't do this.

另外,你还提到了你有“明确的选项”。请不要这样做。

Setting "Option Explicit Off" means that you don't ever have to declare variables. This means that the following code will silently and invisibly create the variable "Y":

设置“选项显式关闭”意味着您不必声明变量。这意味着下面的代码将无声地、不可见地创建变量“Y”:

Dim X as Integer
Y = 3

This is horrible, mad, and wrong. It creates variables when you make typos. I keep hoping that they'll remove it from the language.

这是可怕的、疯狂的、错误的。它会在输入错误时创建变量。我一直希望他们能把它从语言中删除。