How to declare an array inline in VB.NET

Arraysvb.netLiterals

Arrays Problem Overview


I am looking for the VB.NET equivalent of

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

Arrays Solutions


Solution 1 - Arrays

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

Solution 2 - Arrays

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.

Solution 3 - Arrays

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

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

Solution 4 - Arrays

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

Solution 5 - Arrays

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/

Solution 6 - Arrays

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionerikView Question on Stackoverflow
Solution 1 - ArraysgfrizzleView Answer on Stackoverflow
Solution 2 - ArraysJon SkeetView Answer on Stackoverflow
Solution 3 - ArraysNetricityView Answer on Stackoverflow
Solution 4 - ArraysDavid MohundroView Answer on Stackoverflow
Solution 5 - ArraysJesper PalmView Answer on Stackoverflow
Solution 6 - ArraysSteve WrightView Answer on Stackoverflow