34

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.

3
  • What's the point to create a new dynamic length array and then enforce its length? If you look at the C# code it is done during initialization, not declaration like you want to do. Commented Aug 6, 2013 at 8:16
  • 2
    Try using Redim - Redim dest(src.Length) Commented Aug 6, 2013 at 8:17
  • 2
    @Tim - in VB, arrays are specified with upper bound, not length. Must do: Redim dest(src.Length-1). Commented Jan 25, 2018 at 17:52

3 Answers 3

62

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) {} 
Sign up to request clarification or add additional context in comments.

5 Comments

This answer works for arrays of type String, but not on other types.
@Luminous The answer shows a way to initialize an array variable by using an array literal; as far as I know, there is no problem with other types. Have you got any exception?
Well, the question was initializing an empty array with a known variable which is exactly what your answer does. For at least type String. Any other type like double for instance creates an array filled with all 0's instead of null or Nothing. That's what I got when I used your answer.
@Luminous In the case you have mentioned, you should declare a null-able variable like this: Dim dest As Double?() dest = New Double?(10) {}
@Luminous: Creating a new variable in .NET pre-populates that variable with the default value of that Class or Structure (struct in C#). This default value is Nothing (null in C#) for classes, 0 for all numeric structures and Chars and False for Booleans (bool in C#). What you are therefore seeing is the Framework filling the array with the default values; nothing else to it.
9

The normal way to do this would be to declare the array like so:-

Dim my_array() As String 

and later in the code

ReDim my_array (src.Length - 1) 

2 Comments

ReDim parameter is highest index. Here, that would be src.Length - 1 (rather than src.length). Submitting this as an edit.
@ToolmakerSteve, you are quite right; I must get out of my old-fashioned 1-based habits...
5

You can use Redim as already noted but this is the equivalent VB code to your C#

Dim dest As String() dest = New String(src.Length - 1) {} 

Try and avoid using dynamic arrays though. A generic List(Of T) is much more flexible

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.