• 0

[C#] Array of Buttons


Question

16 answers to this question

Recommended Posts

  • 0

Visual Basic 6.0 Supported Control Arrays which is the exactly functionality which you are trying to take advantage of However VB .NET does not, and nor does C#. However you can mimic this functionality programmaticly.

Please Follow this article here:

Creating Control Arrays in Visual Basic .NET and Visual C# .NET

That should help you get started. :)

  • 0
  yaku said:
.Net always starts at 0 so 31 will be 32.

Button[,] buttons = new Button[31, 31];

Not true. That would create a 31x31 matrix.

From the MSDN Array Tutorial:

  Quote
C# arrays are zero indexed; that is, the array indexes start at zero. Arrays in C# work similarly to how arrays work in most other popular languages There are, however, a few differences that you should be aware of.

When declaring an array, the square brackets ([]) must come after the type, not the identifier. Placing the brackets after the identifier is not legal syntax in C#.

int[] table; // not int table[]; 

Another detail is that the size of the array is not part of its type as it is in the C language. This allows you to declare an array and assign any array of int objects to it, regardless of the array's length.

int[] numbers; // declare numbers as an int array of any size

numbers = new int[10];  // numbers is a 10-element array

numbers = new int[20];  // now it's a 20-element array

and

  Quote
Multidimensional Array

int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };

string[,] siblings = new string[2, 2] { {"Mike","Amy"}, {"Mary","Albert"} };

You can omit the size of the array, like this:

int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };

string[,] siblings = new string[,] { {"Mike","Amy"}, {"Mary","Albert"} };

You can also omit the new operator if an initializer is provided, like this:

int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };

string[,] siblings = { {"Mike", "Amy"}, {"Mary", "Albert"} };

  • 0
  yaku said:
Yes it is true.

if you do a button[31] it will yeald 32 buttons.

0 - 31 thats 32 thus 32 buttons last time I counted.

OK, well when i do Button[31] it gives me a 31-item array. (0 to 30)

And when i do Button[31,31] it gives me a 31x31 item array.

private void ArrayCheck()
{
	Button[,] b = new Button[31,31];
	int x = b.Length;
	int y = (int)Math.Sqrt((double)x);
	MessageBox.Show(string.Format("{00} matrix = {1} parts", y, x));
}

Pretty sure that illustrates that what I (and the MSDN documentation) said is correct.

Edit: The resulting output is "31 x 31 matrix = 961 parts", fyi

  • 0

In VB.NET can you make your arrays start/end at whatever you want like you could in VB or is it all zero-based all the time?

I'm guessing by xStainDx's comment, its all zero, all the time, but i was just curious if that was just a default, or the only option.

  • 0

i wouldn't do it myself, i'm quite accustomed to zero-based.

I just found it interesting that in regular VB you could have an array go from like 14 to 73... not sure what purpose that would suit, but i thought it was interesting. (I never tried it, so i might have just been hearing unfounded rumors, but ya)

  • 0
  RufioPan said:
On a slightly unrelated note, why do you have to cast buttons[10,23] as a Button type?  Isn't it already an array of Buttons?  What type does buttons[10,23] return as?

no.. because "buttons" is the array name. "Button" is the type.

  • 0
  RufioPan said:
On a slightly unrelated note, why do you have to cast buttons[10,23] as a Button type? Isn't it already an array of Buttons? What type does buttons[10,23] return as?

in the example of code you quoted, you don't have to cast it, you can just say

buttons[10,23].Text = "Test";

had this not been an actuall "Array of Buttons" and instead some other collection (like an ArrayList, or something of that nature), then all the items are considered objects (so the return value of the indexer is an object) and casting would be necessary. But for this, more basic, type of array, casting is not necessary.

  • 0
  RufioPan said:
On a slightly unrelated note, why do you have to cast buttons[10,23] as a Button type? Isn't it already an array of Buttons? What type does buttons[10,23] return as?

Its was a force of habit. I wrote that example without really thinking about it. it still works. and if you going to say something about a performance hit think again. read the doc and you guys don't have to be that picky about a small quick example. :blink:

  • 0
  yaku said:
read the doc and you guys don't have to be that picky about a small quick example. :blink:

i don't think anyone was being picky - i think RufioPan was just curious and didn't know if it really had to be done that way - so we answered, no biggie :)

  • 0

Button[,] buttons = new Button[32,32];

for (int i = 0; i < buttons.Length; i++)
 ? for (int j = 0; j < buttons.length; j++)
 ? ? ?buttons[i,j] = new Button();

//Reference like:
buttons[0,0];

While arrays do start at index 0, declaring a new array with length 32 makes one with spots for 0 - 31, not 0 - 32.

I'm sure this has already been explained already in this thread, but I wanted to post this just to clear up any confusi:p. :p

Edited by Sn1p3t
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.