Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare an array inline in VB.NET

I am looking for the VB.NET equivalent of

var strings = new string[] {"abc", "def", "ghi"}; 
like image 479
erik Avatar asked Nov 14 '08 21:11

erik


People also ask

How do you declare an array in VB example?

In visual basic, Arrays can be declared by specifying the type of elements followed by the brackets () like as shown below. Dim array_name As [Data_Type](); Here, array_name represents the name of an array and Data_type will represent the data type of elements to store in an array.

What is an array how it is declared in VB?

An array is a set of values, which are termed elements, that are logically related to each other. For example, an array may consist of the number of students in each grade in a grammar school; each element of the array is the number of students in a single grade.

How do you declare an array variable?

To create an array, define the data type (like int ) and specify the name of the array followed by square brackets []. To insert values to it, use a comma-separated list, inside curly braces: int myNumbers[] = {25, 50, 75, 100}; We have now created a variable that holds an array of four integers.

How can you declare an array give syntax?

Array declaration syntax is very simple. The syntax is the same as for a normal variable declaration except the variable name should be followed by subscripts to specify the size of each dimension of the array. The general form for an array declaration would be: VariableType varName[dim1, dim2, ...


2 Answers

Dim strings() As String = {"abc", "def", "ghi"} 
like image 54
gfrizzle Avatar answered Sep 21 '22 13:09

gfrizzle


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.

like image 40
Jon Skeet Avatar answered Sep 19 '22 13:09

Jon Skeet