AVR for .NET offers two types of arrays:

  • Dimmed array. An array whose number of elements are known when it's declared (ie, at compile time). Part 1 of this series covered the basics of dimmed arrays. If you're familiar with AVR Classic's arrays you'll find AVR for .NET's dimmed arrays to be very similar.
  • Ranked array. An array whose number of elements are not known when it's declared (ie, the number of elements in ranked arrays are set at runtime).

AVR Classic offered only dimmed arrays (arrays of fixed size). This constraint often caused arrays in AVR Classic to be declared much larger than necessary. For example, consider needing to populate an array with the names of files in a folder. In some cases, there may be just a few files in that folder; in other cases there may be hundreds. To be safe in AVR Classic, you'd need to declare the array with enough elements to hold the maximum number of files you'd expect the folder to contain. Not only was this nearly always very wasteful, guessing too low would cause the program to fail. AVR for .NET's dynamically-sized arrays, its ranked arrays, allow for that array of file names to be correctly sized every time.

Declaring variably-sized arrays

To declare an array of a known number of elements, you use the Rank() keyword instead of Dim(). Where Dim() specifies the number of elements. When declaring a dynamically sized array, you use the Rank() keyword to specify the number of dimensions. (Yes, it's confusing! A poor choice that VB made a long time ago lead to this confusing naming). The Dim() and Rank() keywords are mutually exclusive—use one or the other.

The code below declares a single dimension array with 10 elements:

DclArray MyFiles Type(*String) Dim(10)

This array can only contain ten elements, no more, no less.

The code below declares an array of a single dimension with an unknown number of elements:

DclArray MyFiles Type( *String ) Rank( 1 )

This array can contain any number of elements and its number of elements may change multiple times during runtime. Although the number of elements in a ranked array can vary at runtime, the number of dimensions is fixed with the number of dimensions specified with the Rank() keyword.

Using a breakpoint right after the above declaration shows that at runtime, immediately after the declaration, that the array has no elements and its value is *Nothing.


Figure 1 shows how a ranked array looks in Visual Studio immediately after its declaration.

Ranked arrays are often used to store a varying size list of array elements as returned from a function. Many functions in the .NET Framework return dynamically-sized arrays (and we'll soon see how you can write your own functions to return a dynamically-sized array). Let's revisit the need to fetch a list of file names from a folder. Here's a good way to do that in AVR for .NET:

DclArray MyFiles Type( *String ) Rank( 1 )

MyFiles = System.IO.Directory.GetFiles("c:\pdfs")


Figure 2. A ranked array populated with file names in a given folder.

Regardless of the number of files in the c:\pdfs folder MyFiles will always have one element for each file in the folder each time GetFiles() is called.

Explicitly populating a ranked array

To explicitly populate a ranked array, you need to create a new instance of the array. For example, let's use a variable named MemberCount to determine, at runtime, how many elements the Members array should have. To do this, you'd use this code:

DclFld MemberCount Type(*Integer4) 
DclArray Members Type(*String) Rank(1) 

MemberCount = 4
Members = *New *String[MemberCount]


Figure 3. After creating a new instance of the Members array it has four elements.

After creating the Members array, you can now assign the four elements' values.

Note that MemberCount is wrapped in brackets [] immediately after the array type. Parentheses will not work in this case. The line below will not compile:

Members = *New *String(MemberCount)

This is yet another good reason to always use brackets to indicate array subscripts. There are some places where AVR for .NET will accept either parentheses or brackets—it is better to always use brackets to indicate array subscripts.

A ranked array subtly

A subtle ranked array rule is at play here: a ranked array's size is immutable--once a number of elements is assigned to it, it can't be changed. However, you can re-instance the array to a number of elements as many times as necessary. For example, consider the following code:

DclArray MyFiles Type( *String ) Rank( 1 )

MyFiles = System.IO.Directory.GetFiles("c:\pdfs")
// Do something here with the file names returned. 

MyFiles = System.IO.Directory.GetFiles("c:\docs")
// Do something here with the file names returned.

Each time GetFiles() is called, the array it returns is instanced to the correct size and a reference to that new instance is assigned to MyFiles.

Dimmed arrays, by contrast, must always have the number of elements with which the dimmed array was declared.

Ranked array shorthand

When declaring ranked arrays, there is a shorthand available that instances and populates a ranked array with a single line of code. For example, this code:

DclArray Members Type(*String) Rank(1)

Members = *New *String[] {"John", "George", "Ringo", "Paul"}

first declares a ranked array of type *String. The second line then implicitly instances a four-element array with the values specified in braces. The number of values specified in the braces determines how many elements will be in the array.

The code above is semantically identical to doing this:

DclArray Members Type(*String) Rank(1)

Members = *New *String[4]

Members[0] = "John"
Members[1] = "George"
Members[3] = "Ringo"
Members[4] = "Paul"

Creating a function that returns a ranked array

You'll usually want to bury the details of how a ranked array gets populated in a function. For example, consider how clean this code is:

DclArray MyFiles Type( *String ) Rank( 1 )

MyFiles = System.IO.Directory.GetFiles("c:\pdfs")

The code doesn't care how the array of file names was populated. Here is a function that hides the logic of getting the names of the Beatles:

BegFunc GetBeatlesNames Type(*String) Rank(1)   
    DclArray Members Type(*String) Rank(1)

    Members = *New *String[] {"John", "George", "Ringo", "Paul"}

    LeaveSr Members
EndFunc 

To return a ranked array from a function, specify the function's type with the Type() keyword and then specify the number of dimensions of an array of that type. Note that you never specify the number of elements to return; you only specify the array's dimensions. Calling the GetBeatlesNames() function is pretty predictable:

DclArray Members Type(*String) Rank(1)

Members = GetBeatlesNames() 

A ranked array conundrum awaits you if you want to populate a ranked array by reading a variable number of records in a file. The problem is, you can't instance the ranked array until you've read the variable number of records because you wouldn't know how many elements the ranked array should be instanced with. Ranked arrays aren't like collections, you can't just dynamically add elements to them; you must first instance the ranked array to the correct number of elements.

A way to resolve this conundrum is to read as many records as required and save the values from each in a collection. Then, convert that collection to an array as the function's return value. This version of the GetBeatlesNames() function is shown below:

BegFunc GetBeatlesNames Type(*String) Rank(1)   
    DclArray Members Type(*String) Rank(1)

    DclFld BeatlesCollection Type(System.Collections.Specialized.StringCollection) New()

    SetLL Beatles Key(*Start) 
    Read Members
    DoUntil (Members.IsEof)
        BeatlesCollection.Add(Name) 
        Read Members
    EndDo 

    Members = *New *String[BeatlesCollection.Count]

    BeatlesCollection.CopyTo(Members,0) 

    LeaveSr Members
EndFunc 

In this case, the BeatlesCollection has a member added for each record read. Then the two lines first instance the Members array to the correct number of elements and then uses the StringCollection's CopyTo() method to copy the collection to the array.

    Members = *New *String[BeatlesCollection.Count]

    BeatlesCollection.CopyTo(Members,0)

Techniques vary, based on the collection type, for how to convert the collection to an array. Read the documentation for the collection type you're using to see how to do this. For simple string uses, the StringCollection used here works pretty well.

Why not just return the collection? In many cases you might, but there are also many cases where it is leaner and cleaner to return a simple array.

In Part 3, we'll take a look at AVR for .NET array methods and properties.