TEMPESTINI.NET

Observations in Software Development

Posts Tagged ‘Arrays’

GetUpperBound

Posted by Rick Tempestini on March 12, 2008

The GetUpperBound method of an array actually takes a single parameter in .NET. Unfortunately, Microsoft’s documentation on MSDN does not provide a clear explanation of the purpose and usage of this parameter (in my opinion).

First, the GetUpperBound method of an array will return an integer with the number corresponding to the highest number “element” that can be placed into a array. For example, Label1 would display the number ’4′ after the following snippet of code because the array was created to hold five (5) values (remember, arrays in .NET are zero-based)

GetUpperBoundSingle2

In the code above, however, a zero (0) was used when calling the GetUpperBound method. Since the array was declared as a single-dimensional array, there was only one dimension to get the upper bound value from. Therefore, a zero (0) was passed in to represent the first (and only) dimension of this array. Note: dimensions of the array are even zero-based and that is why a one (1) was not passed in.

On a two-dimensional array, this parameter can now take a zero or a one based upon which dimension you want to return a value for as seen in the following example:

GetUpperBoundMulti

In the code above, a two-dimensional array is created with three (3) as the first dimension and four (4) as the second dimension. Again, since the dimensions of an array are zero-based, the call to GetUpperBound(0) will represent the first dimension and the call to GetUpperBound(1) will represent the second dimension.

Posted in Other | Tagged: , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.