Thread: Array of Runtime Labels

  1. #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?

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Add labelTest2[0] = new Label();

    before you call its properties.

    You've created an array of 10 Labels, not 10 Labels.

  3. #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!

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM