How to declare an inline array in VB.NET

I am looking for the equivalent of VB.NET

var strings = new string[] {"abc", "def", "ghi"}; 
+49
arrays literals
Nov 14 '08 at 21:04
source share
6 answers
 Dim strings() As String = {"abc", "def", "ghi"} 
+69
Nov 14 '08 at 21:06
source share

There are many correct answers to this right now, but here is the "teach a guy to catch" version.

First, create a tiny console application in C #:

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

Compile it, saving the debug information:

 csc /debug+ Test.cs 

Launch Reflector and open the Main method, and then decompile VB. You get:

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

Thus, we got the same answer, but without knowledge of VB. This will not always work, and there are many other conversion tools, but this is a good start. Definitely worth a try as the first port of call.

+38
Nov 14 '08 at 21:16
source share

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

 Dim strings = {"abc", "def", "ghi"} 
+6
Apr 19 '12 at 17:21
source share
 Dim strings As String() = New String() {"abc", "def", "ghi"} 
+5
Nov 14 '08 at 21:05
source share

Not a VB guy. But maybe something like this?

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

(About 25 seconds later ...)

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

+5
Nov 14 '08 at 21:06
source share

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

+3
Nov 14 '08 at 21:06
source share



All Articles