Reputation: 2134
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.
Upvotes: 1
Views: 619
Reputation: 4399
As Ian says, you need to specify each element when initializing to a non-default value. What you're doing is, you're trying to initialize the "Location, Text, Width, and Height" properties of the TextBox
array, but the array does not have those properties, TextBox
does. If you want to "initialize*" an array with a non-default value, best way would be to use a for-loop as in:
const int numOfElements = 8;
TextBox[] textBoxArray = new TextBox[numOfElements];
for (int i = 0; i < numOfElements; i++)
{
textBoxArray[i] = new TextBox() { Location = new Point(15, 50), Text = "55", Width = 30, Height = 30 };
}
*: This is not really initializing, as your array was already initialized with default value when you declared it. It's more of "populating" I guess.
Upvotes: 1
Reputation: 6920
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:
Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor.
class Cat
{
// Auto-implemented properties.
public int Age { get; set; }
public string Name { get; set; }
}
Cat cat = new Cat { Age = 10, Name = "Fluffy" };
The collection initializer, which you use in your second case, works like this:
Collection initializers let you specify one or more element initializers when you initialize a collection class that implements IEnumerable. The element initializers can be a simple value, an expression or an object initializer. By using a collection initializer you do not have to specify multiple calls to the Add method of the class in your source code; the compiler adds the calls.
List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
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:
TextBox[] stamText = new TextBox[8]
{
new TextBox() { Location = new Point(15, 50), Text = "55", Width = 30, Height = 30 },
new TextBox() { Location = new Point(15, 50), Text = "55", Width = 30, Height = 30 },
new TextBox() { Location = new Point(15, 50), Text = "55", Width = 30, Height = 30 },
new TextBox() { Location = new Point(15, 50), Text = "55", Width = 30, Height = 30 },
new TextBox() { Location = new Point(15, 50), Text = "55", Width = 30, Height = 30 },
new TextBox() { Location = new Point(15, 50), Text = "55", Width = 30, Height = 30 },
new TextBox() { Location = new Point(15, 50), Text = "55", Width = 30, Height = 30 },
new TextBox() { Location = new Point(15, 50), Text = "55", Width = 30, Height = 30 }
};
Upvotes: 4