471,618 Members | 1,153 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,618 software developers and data experts.

Count 2D array

Jon
Hello all,

I have a 2D array called Info[,]. This is of variable size and I'm
struggling to find out how to count the number of elements in the first
dimention.

I've tried all sorts. Can anyone please help?

Thanks,

Jon
Feb 21 '06 #1
2 2272
Hi Jon,

Use the GetUpperBound(index) method as in the code below, this gives you the
highest index, in this case 4 (zero based), hence 5 elements.

int[,] myarray = new int[5, 2];

myarray[0, 0] = 0; myarray[0, 1] = 1;
myarray[1, 0] = 2; myarray[1, 1] = 3;
myarray[2, 0] = 4; myarray[2, 1] = 5;
myarray[3, 0] = 6; myarray[3, 1] = 7;
myarray[4, 0] = 8; myarray[4, 1] = 9;

int ele = myarray.GetUpperBound(0) + 1;

Console.WriteLine("The first index contains {0} elements", ele);
Console.Read();

Hope this helps

Publicjoe

C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Tutorial at http://www.publicjoe.f9.co.uk/vbnet/vbnet.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html

"Jon" <Jo*@discussions.microsoft.com> wrote in message
news:AE**********************************@microsof t.com...
Hello all,

I have a 2D array called Info[,]. This is of variable size and I'm
struggling to find out how to count the number of elements in the first
dimention.

I've tried all sorts. Can anyone please help?

Thanks,

Jon

Feb 21 '06 #2
Jon
Thanks mate, just the job!

"Publicjoe" wrote:
Hi Jon,

Use the GetUpperBound(index) method as in the code below, this gives you the
highest index, in this case 4 (zero based), hence 5 elements.

int[,] myarray = new int[5, 2];

myarray[0, 0] = 0; myarray[0, 1] = 1;
myarray[1, 0] = 2; myarray[1, 1] = 3;
myarray[2, 0] = 4; myarray[2, 1] = 5;
myarray[3, 0] = 6; myarray[3, 1] = 7;
myarray[4, 0] = 8; myarray[4, 1] = 9;

int ele = myarray.GetUpperBound(0) + 1;

Console.WriteLine("The first index contains {0} elements", ele);
Console.Read();

Hope this helps

Publicjoe

C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Tutorial at http://www.publicjoe.f9.co.uk/vbnet/vbnet.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html

"Jon" <Jo*@discussions.microsoft.com> wrote in message
news:AE**********************************@microsof t.com...
Hello all,

I have a 2D array called Info[,]. This is of variable size and I'm
struggling to find out how to count the number of elements in the first
dimention.

I've tried all sorts. Can anyone please help?

Thanks,

Jon


Feb 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Scott Castillo | last post: by
2 posts views Thread by Dan =o\) | last post: by
3 posts views Thread by chrisperkins99 | last post: by
68 posts views Thread by Martin Joergensen | last post: by
5 posts views Thread by colin | last post: by
4 posts views Thread by =?Utf-8?B?a2s=?= | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.