Collections

Arrays

Arrays provide a means to hold a strongly typed collection of objects. They can bee Array function:

Dim xs = Array(1, 2, 3, 4)

... or declared and initalized incrementally like so:

Dim xs(2)
xs(0) = -1
xs(2) = 1

... which creates an array with valid indexes ranging from 0 to 2. Unitialized indexes have the default value for the type (0 for numbers, Nothing for reference types, etc). Arrays can be indexed like so:

Dim xs = Array(0, 1, 2, 3)

' True
xs(2) > 0

' False
xs(0) > 0

Type Annotations

For variables, function parameters and properties:

' Array of integers
Dim xs() As Int32

' Array of strings
Dim adjectives() As [System.String]

Function return types have their own rules, but parameters follow the same rules as variables:

Function MapInt(fn, xs() As Int32) As Int32()

Function MapString(fn, strings() As [System.String]) As [System.String]()

ReDim

Warning

ReDim and ReDim Preserve are inherited from VBScript. You should use a collection that handles resizing (like ArrayList) rather than doing it yourself.

ReDim and ReDim Preserve allow you to change the size of an array:

' An array with one cell
Dim xs(0)

' Resizes it to have two cells
ReDim xs(1)



Dim ys = Array(1, 2, 3)

' Resizes the array and preserves the contents
ReDim Preserve ys(5)

Utilities

It’s good to remember that Wasabi’s arrays are simply .NET arrays. Consequently, you can use the utilities provided by System.Array when working with them:

Dim xs = Array(1, 2, 3, 4)
[System.Array].IndexOf(xs, 3)
[System.Array].Sort(xs)

FogCreek.Core.Arrays also provides a number of utility functions that are useful when working with arrays:

Dim xs = Array(-4, -3, -2, -1, 0, 1, 2, 3, 4)
Dim ys = Arrays.FilterInt32(xs, Lambda (x) x > 0)

' Convert a .NET collection into a strongly-typed array
Dim bugs = Arrays.ArrayFromICollectionCBug(list)

Dictionaries

Dictionaries provide a way to map string keys to a strongly typed collection of objects. They can be created using the Dictionary function:

Dim dict{} As String = Dictionary()

Elements are set and accessed using indexers:

dict("msg") = "Hello world!"
dict("name") = "Aaron"

%><%= dict("name") %><%
class Dictionary
Add(key, value)

Adds a key-value pair to the dictionary.

Exists(key)

Returns true if the key exists in the dictionary.

Items()

Returns an array of the values stored in the dictionary.

Keys()

Returns an array of the keys stored int he dictionary.

Remove(key)

Removes the key and associated value from the dictionary.

RemoveAll()

Empties the dictionary.

Table Of Contents

Previous topic

Functions & Subroutines

Next topic

Classes

This Page