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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5262577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:20:41  来源:igfitidea点击:

Declaring and initializing a string array in VB.NET

vb.netarraysdata-structures

提问by chris

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?

我想我不清楚 {} 的含义 - 有没有办法隐式返回字符串数组而不显式创建和初始化它?

采纳答案by pirho

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 中的集合和数组初始值设定项

回答by msarchet

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.NET 9.0),因此您必须声明创建新实例

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

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

回答by Frosty840

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

我相信您需要指定“Option Infer On”才能使其工作。

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.

Option Infer 允许编译器猜测您的代码所表示的内容,因此它会猜测 {"stuff"} 是一个字符串数组。随着“选项推断关”,{“东西”}不会有分配给它,任何类型不断,所以它总是会失败,没有一个类型说明符。

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

Option Infer 是,我认为在新项目中默认为 On,但当您从早期框架迁移到 3.5 时默认为 Off。

Opinion incoming:

意见传入:

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

另外,你提到你有"Option Explicit Off"不要这样做。

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

设置“Option Explicit Off”意味着您不必声明变量。这意味着以下代码将默默地、不可见地创建变量“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.

这是可怕的、疯狂的和错误的。当您打错字时,它会创建变量。我一直希望他们能从语言中删除它。