VB.NET How to declare new empty array of known length

arrays, vb.net

Solution

The syntax of VB.NET in such a case is a little different. The equivalent of

string[] dest;
// more code here
dest = new string[src.Length];

is

Dim dest As String()
' more code here
dest = New String(src.Length - 1) {}

Syntax note

When you use Visual Basic syntax to define the size of an array, you specify its highest index, not the total number of elements in the array. learn.microsoft.com

Example

These two array both have a length of 5:

C#:
string[] a = new string[5];
VB: 
Dim a As String() = New String(4) {}

Problem

Is there a way in VB.NET to declare an array, and later initialize it to a known length in the code? In other words, I'm looking for the VB.NET equivalent of the following C#.NET code: ``` string[] dest; // more code here dest = new string[src.Length]; ``` I tried this in VB, and it didn't work. ``` Dim dest() as string ' more code here dest = New String(src.Length) ``` What am I missing? NOTE: I can confirm that ``` Dim dest(src.Length) as string ``` works, but is not what I want, since I'm looking to separate the declaration and initialization of the array.

Original source