I am looking for the VB.NET equivalent of
var strings = new string[] {"abc", "def", "ghi"};
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.
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.
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.
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, ...
Dim strings() As String = {"abc", "def", "ghi"}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With