typestar

Arrays in Visual Basic

Literals, 2-D grids and ReDim Preserve, VB's in-place resize.

Dim primes() As Integer = {2, 3, 5, 7, 11}
Console.WriteLine(primes.Length)
Console.WriteLine(primes(0) + primes(4))

Dim grid(2, 2) As Integer
grid(1, 1) = 9
Console.WriteLine($"center: {grid(1, 1)}")

ReDim Preserve primes(6)
primes(5) = 13
primes(6) = 17
For Each p In primes
    Console.Write($"{p} ")
Next
Console.WriteLine()

How it works

  1. {2, 3, 5} literals size the array; indexes go in parentheses.
  2. Dim grid(2, 2) declares by upper bound — that is a 3-by-3 grid.
  3. ReDim Preserve grows an array and keeps what it held.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
326
Tokens
103
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 69 seconds.

Type this snippet

Step 1 of 3 in Collections, step 4 of 29 in Language basics.

← Previous Next →

Arrays in other languages