![]() |
| | #1 |
| Polar Fuzz Join Date: Oct 2003
Posts: 36
| Array of Runtime Labels Code: namespace sud
{
public partial class sud : Form
{
private Label labelTest = new Label();
public sud()
{
InitializeComponent();
}
private void sud_Load(object sender, EventArgs e)
{
this.labelTest.Size = new System.Drawing.Size(50, 50);
this.labelTest.Location = new System.Drawing.Point(40, 40);
this.labelTest.Name = "cellTest";
this.labelTest.Text = "Test";
this.Controls.Add(labelTest);
}
} // end partial class: sud
} // end namespace: sud
Code: namespace sud
{
public partial class sud : Form
{
private Label[] labelTest2;
public sud()
{
InitializeComponent();
}
private void sud_Load(object sender, EventArgs e)
{
this.labelTest2 = new Label[10];
this.labelTest2[0].Size = new System.Drawing.Size(50, 50);
this.labelTest2[0].Location = new System.Drawing.Point(40, 40);
this.labelTest2[0].Name = "cellTest";
this.labelTest2[0].Text = "Test";
this.Controls.Add(labelTest2[0]);
}
} // end partial class: sud
} // end namespace: sud
|
| wbeasl is offline | |
| | #2 |
| Registered User Join Date: Jun 2008 Location: RING 0
Posts: 468
| Add labelTest2[0] = new Label(); before you call its properties. You've created an array of 10 Labels, not 10 Labels. |
| valaris is offline | |
| | #3 |
| Polar Fuzz Join Date: Oct 2003
Posts: 36
| I still got the same error but I did manage to get it to work from that suggestion. Changes in blue: Code: namespace sud
{
public partial class sud : Form
{
private Label[] labelTest2;
public sud()
{
InitializeComponent();
labelTest2 = new Label[10];
}
private void sud_Load(object sender, EventArgs e)
{
this.labelTest2[0] = new Label();
this.labelTest2[0].Size = new System.Drawing.Size(50, 50);
this.labelTest2[0].Location = new System.Drawing.Point(40, 40);
this.labelTest2[0].Name = "cellTest";
this.labelTest2[0].Text = "Test";
this.Controls.Add(labelTest2[0]);
}
} // end partial class: sud
} // end namespace: sud
|
| wbeasl is offline | |
| | #4 |
| Registered User Join Date: Jun 2008 Location: RING 0
Posts: 468
| Yup. You had 10 references to a label. It's analgous to in C++ creating 10 pointers to something and trying to access an element of it. It's not going to fly unless you allocate memory to it. |
| valaris is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| from 2D array to 1D array | cfdprogrammer | C Programming | 17 | 03-24-2009 10:33 AM |
| Help with Searching Array for Longest Sequence of an Element | w2look | C Programming | 7 | 11-25-2008 01:50 AM |
| 1-D array | jack999 | C++ Programming | 24 | 05-12-2006 07:01 PM |
| Class Template Trouble | pliang | C++ Programming | 4 | 04-21-2005 04:15 AM |
| Array Program | emmx | C Programming | 3 | 08-31-2003 12:44 AM |