C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-20-2008, 09:05 PM   #1
Polar Fuzz
 
Join Date: Oct 2003
Posts: 36
Array of Runtime Labels

Using C# 2005, I am trying to create labels at run-time using an array. The code below WITHOUT the array does work without run-time errors:

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
I get this error ONLY at run-time: object reference not set to an instance of an object if I use the code below which uses an ARRAY to create the labels.

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
Suggestions?
wbeasl is offline   Reply With Quote
Old 12-20-2008, 10:45 PM   #2
Registered User
 
valaris's Avatar
 
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   Reply With Quote
Old 12-20-2008, 11:26 PM   #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
Thanks for your help!
wbeasl is offline   Reply With Quote
Old 12-20-2008, 11:53 PM   #4
Registered User
 
valaris's Avatar
 
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:29 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22