How to declare an array inline in VB.NET

烂漫一生 提交于 2019-12-04 08:47:45

问题


I am looking for the VB.NET equivalent of

var strings = new string[] {"abc", "def", "ghi"};

回答1:


Dim strings() As String = {"abc", "def", "ghi"}



回答2:


There are plenty of correct answers to this already now, but here's a "teach a guy to fish" version.

First create a tiny console app in C#:

class Test
{
    static void Main()
    {
        var strings = new string[] {"abc", "def", "ghi"};
    }
}

Compile it, keeping debug information:

csc /debug+ Test.cs

Run Reflector on it, and open up the Main method - then decompile to VB. You end up with:

Private Shared Sub Main()
    Dim strings As String() = New String() { "abc", "def", "ghi" }
End Sub

So we got to the same answer, but without actually knowing VB. That won't always work, and there are plenty of other conversion tools out there, but it's a good start. Definitely worth trying as a first port of call.




回答3:


In newer versions of VB.NET that support type inferring, this shorter version also works:

Dim strings = {"abc", "def", "ghi"}



回答4:


Dim strings As String() = New String() {"abc", "def", "ghi"}



回答5:


Not a VB guy. But maybe something like this?

Dim strings = New String() {"abc", "def", "ghi"}

(About 25 seconds late...)

Tip: http://www.developerfusion.com/tools/convert/csharp-to-vb/




回答6:


Dim strings As String() = {"abc", "def", "ghi"}



来源:https://stackoverflow.com/questions/291413/how-to-declare-an-array-inline-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!