Create a Jagged Array

Problem

You need to create a nonrectangular array. For example, you might want to create a two-dimensional array in which different rows have different numbers of column elements.

Solution

Don’t attempt this unless required for compatibility reasons—it makes your code less usable and much more obscure. If required, use the Array.CreateInstance method.

Discussion

Jagged arrays (sometimes described as arrays of arrays) introduce complexity, and should be used sparingly. However, they do have legitimate uses and are supported by Visual Basic .NET.

One way to create a multidimensional array is by including multiple sets of closed brackets when you define the array. As an example, the following code creates an array with 12 rows (one ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.