Visual Basic .NET/Arrays

Simple arrays
An array is simply a variable that can store more than one piece of data. The data is stored in a list. If you declare an integer, then that variable can only store one integer. An array of integers can store many integers. Each one is given its own number. For example, this line of code:

will give an array like this

This is similar to declaring a normal variable with one difference, the upperBound argument. Unlike previous versions versions of Visual Basic, all arrays in Visual Basic .NET start at 0 and go to the upperBound, for Example:

will declare a variable called MyArray with space for 11 Integers. The integers are stored in MyArray(0), MyArray(1),..., MyArray(10).

To declare an array and assign values in a single line, use the following

Another way to create an array is as follows

Note that anytime these lines are used:

This means the same thing, and is easier to use:

The (number) after the name of the variable declares it as a string.

To access one of the elements in the array:

How many elements do we have in the array?

Display all the elements in the array.

Sort the array.

Sort in descending order.

Reverse the array.

Now that we have, here is another way to sort in descending order without.

With Option Strict On, we would need to use SetValue to assign array elements.

Multi-Dimensional arrays
Arrays also can be multi-dimensional - 2D, 3D, 4D etc. For example, this line of code:

Will give an array like this:

2D arrays can be used in games like a Tic-Tac-Toe.

3D arrays can be thought of as an array of 2D arrays. Where a 2D array has rows and columns, as in a game of Tic Tac Toe, the 3D array stores multiple grids of Tic Tac Toe.

To declare a 3D array:

In this example, the first dimension would typically be the number of arrays in this 3D array. We know these are 2D arrays as the second dimension specifies how many rows in these arrays and the third dimension the number of columns.

Arrays manipulations methods

 * : to transform a string into an array, according to the separator which is placed in parameter.
 * : to convert an array into a string.
 * : to filter some array entries.

Example: