Skip to content
Lesson 6 of 7

Step 1 of 5 · Reading · ~2 min

Learn

Subs, Functions, Arrays

VB.NET has both fixed arrays and modern collections (List, Dictionary).

Arrays

Dim nums(4) As Integer                       ' indices 0..4 — FIVE elements
Dim names() As String = {"Ada", "Bob", "Carol"}

nums(0) = 10
Console.WriteLine(nums(0))                   ' 10
Console.WriteLine(names.Length)              ' 3

Beware: Dim nums(4) declares an array whose highest index is 4, so it holds 5 elements. C# int[4] holds 4. Indexing uses parentheses, not brackets, which means nums(0) and a function call look identical — VB works out which from the name.

Reading past the end raises IndexOutOfRangeException; there is no silent default. Guard with If i >= 0 AndAlso i < nums.Length Then. Indexing itself is constant time — .NET arrays are one contiguous block.

Length: nums.Length is the count; UBound(nums) is the highest valid index, one less. Mixing them up is the classic off-by-one here.

ReDim

Resize an array — contents are lost unless you say Preserve:

Dim nums() As Integer = {1, 2, 3}
ReDim Preserve nums(9)            ' now 10 elements, first 3 kept

Multi-dimensional

Dim matrix(2, 2) As Integer                  ' 3x3
matrix(1, 1) = 5
Dim grid(,) As Integer = {{1, 2, 3}, {4, 5, 6}}

List(Of T)

A growable array, and usually the better default:

Imports System.Collections.Generic

Dim list As New List(Of Integer)
list.Add(3)
list.Add(1)
list.Sort()                       ' in place, ascending
Console.WriteLine(list.Count)     ' 2
Console.WriteLine(list(0))        ' 1

For Each n As Integer In list
    Console.WriteLine(n)
Next

list.Remove(3) removes the first item equal to 3; list.RemoveAt(3) removes the item at index 3. They are one keystroke apart and mean different things.

Dictionary(Of K, V)

A hash map — keys unique, lookup constant time, order not defined:

Dim ages As New Dictionary(Of String, Integer)
ages("Bob") = 25                  ' adds or overwrites
ages.Add("Ada", 36)               ' throws if "Ada" already exists

Console.WriteLine(ages.ContainsKey("Ada"))   ' True
Console.WriteLine(ages("Ada"))               ' 36

For Each kvp As KeyValuePair(Of String, Integer) In ages
    Console.WriteLine(kvp.Key & ": " & kvp.Value)
Next

Since enumeration order is not defined, any output that must be sorted has to be sorted explicitly. The lambda-free way — and the way this course's compiler needs — is to copy the keys into a List(Of String) and call .Sort():

Dim keys As New List(Of String)
For Each k As String In ages.Keys
    keys.Add(k)
Next
keys.Sort()

A note on LINQ

Real VB.NET has full LINQ (nums.Where(...), From n In nums Where n > 3), and you will meet it constantly outside this course. It is built on lambda expressions, which the VB 9-era compiler behind these exercises does not accept, so nothing here uses it. Explicit loops plus List(Of T).Sort do the same jobs.

Your exercise

Count words with a Dictionary(Of String, Integer): ContainsKey tells you whether to start at 1 or add 1. Then sort — the dictionary will not do it for you — by copying counts.Keys into a List(Of String) and calling .Sort(), and print <word>: <count> for each.

Up nextStringsSubs, Functions, Arrays

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…