Make arrays with non-zero lower bounds in C#

In C#, arrays have 0 as their lower bounds. This is true for arrays declared as in the following:

string[] names = new string[10];
string[,] phone_numbers = new int[10, 2];

However, sometimes it would be more convenient to use an array with non-zero lower bounds. In that case, you can make an Array object instead of a normal array.

The Array class has a CreateInstance method that makes an array. It has several overloaded versions, one of which takes as parameters two arrays of integers. The first array gives the number of elements that the array should have in each of its dimensions. The second array gives the lower bound for each dimension.

For example, the following code creates an Array of ints. The first dimension has 10 elements and lower bound 2010. The second dimension contains 4 elements and lower bound 1. This array is intended to hold sales figures for the years 2010 through 2019, in quarters 1 through 4.

// Make an array data[2010..2019, 1..4].
Array data = Array.CreateInstance(typeof(int),
new int[] { 10, 4 },
new int[] { 2010, 1});

Use the Array's SetValue and GetValue methods to get and set values. Use its GetLowerBound and GetUpperBound methods to learn about the Array's bounds.

The example program uses the following code to demonstrate this kind of array.

private void Form1_Load(object sender, EventArgs e)
{
// Make an array data[2010..2019, 1..4].
Array data = Array.CreateInstance(typeof(int),
new int[] { 10, 4 },
new int[] { 2010, 1});

// Initialize the data randomly.
Random rand = new Random();
for (int year = 2010; year <= 2019; year++)
{
for (int quarter = 1; quarter <= 4; quarter++)
{
data.SetValue(rand.Next(10, 99), year, quarter);
}
}

// Display the data.
string txt = "Year\tQ1\tQ2\tQ3\tQ4";
for (int year = data.GetLowerBound(0); year <= data.GetUpperBound(0); year++)
{
txt += "\r\n" + year.ToString();
for (int quarter = data.GetLowerBound(1); quarter <= data.GetUpperBound(1); quarter++)
{
txt += string.Format("\t{0}", data.GetValue(year, quarter));
}
}
txtData.Text = txt;
txtData.Select(0, 0);
}

This code creates an Array as described earlier. It then loops through the array's entries, using the SetValue method to place random values in the array.

The program then loops through the Array again, this time using GetLowerBound and GetUpperBound to learn the Array's bounds. It writes the data into a string and displays the result in a TextBox.

Rant: Note that Visual Basic 6 had the ability to declare arrays with non-zero lower bounds as normal variables. This made using these arrays easy and extremely useful. Microsoft dropped support for that kind of array when it created .NET. It's a shame because the mathematics behind implementing such an array is practically trivial but using the Array class in this way is a big hassle. It was a bad decision on Microsoft's part. (I suspect the reason they did this is that C++ doesn't have arrays with non-zero bounds.)

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.