I need a few control Arrays for my project but my code is generating errors.
Here is how I give a normal control properties (this code works fine)
TextBox stamText = new TextBox()
{
Location = new Point(15, 50),
Text = "55",
Width = 30,
Height = 30
};
Here is the code I tried to do the same but for an array of controls
TextBox[] stamText = new TextBox[8]
{
Location = new Point(15, 50),
Text = "55",
Width = 30,
Height = 30
};
I get errors after each property in the {} saying “;” is expected.
Does anyone know how to solve this problem (giving control arrays properties)?
***EXTENDED
Okay so my program is a form which keeps track of players stats while we tabletop roleplay.
So say I am in a room with Bill, Jim, and I
I click the “Add player” button on the form and it adds a player along with a bunch of controls to do with that player
*AddPlayer Button clicked*
Addplayer()
Public void AddPlayer()
{
*Add a bunch of controls*
checkbox(i)
textbox(i)
}
i += 1
Now everyone got hit a 10. So i go to alter everyones stamina by -10
if for i = 0 to players added
if checkbox(i) = checked then textbox(i).text = (text - 10)
So i need them to be arrays so i can alter multiple peoples stats at once by going through a for loop.
You seem to have a confusion about the object and collection intializers. The object intializer, which you are using in your first case, works likes this:
The collection initializer, which you use in your second case, works like this:
In your example that is throwing the error, you are creating an array of 8 textboxes with the collection intializer but you are passing in the object intializer instead. The reason you get the error about the
;is because the;is used to separate the objects you are adding into the collection. The compiler expects to see 8 textboxes being added in the collection, each separated by a;. Here is one way you could fix this: