"Varangian" <of****@gmail.com> a écrit dans le message de news:
11**********************@o13g2000cwo.googlegroups. com...
| ImageButton[] ship;
| ship = new ImageButton[5];
|
| for (int i=0; i<5; i++)
| {
| ship[i].ImageUrl = pathofImage;
| ship[i].ID = "ShipNo" + i.ToString();
| ship[i].Click += new ImageClickEventHandler(this.ImageBtn_Click);
| this.Form1.Controls.Add(ship[i]);
| }
|
|
| I have this peace of code on the start page... and is giving me an
| error "Object reference not set to an instance of an object"... I
| cannot see what I'm doing wrong. the Array seems good
ship = new ImageButton[5];
This code only allocates an array of references to five ImageButtons, it
doesn't create any ImageButtons.
You have to create a new object for each item in the array.
for (int i=0; i<5; i++)
{
ship[i] = new ImageButton();
ship[i].ImageUrl = pathofImage;
ship[i].ID = "ShipNo" + i.ToString();
ship[i].Click += new ImageClickEventHandler(this.ImageBtn_Click);
this.Form1.Controls.Add(ship[i]);
}
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer