The following works, so I guess you need to work from it to see what the difference is:
private void Form1_Load(object sender, EventArgs e) { int count = 5; Label[] periodlabel = new Label[count]; for (int i = 0; i < count; i++) { periodlabel[i] = new Label(); periodlabel[i].Text = string.Format("Hello {0}", i); } Console.WriteLine(periodlabel[0].Text); } Actually, scratch that, it looks like you are declaring periodlabel twice: > System.Windows.Forms.Label[] periodlabel = new > System.Windows.Forms.Label[10]; > > then at the bottom > > private System.Windows.Forms.Label[] periodlabel; Jonathan Paul wrote: > Hi, > > Got a fun one here. > > In the Designer file, I have defined my array of labels like this > > System.Windows.Forms.Label[] periodlabel = new > System.Windows.Forms.Label[10]; > > then at the bottom > > private System.Windows.Forms.Label[] periodlabel; > > The reason for this is that I want to be able to create on the between 1 > and 10 labels. The problem is this... > > My source looks like this > > public partial class lessonplan : Form > { > public lessonplan(int noperiods) > { > float periodSize = 100.0F / (float)noperiods; > InitializeComponent(); > this.lesson.ColumnCount = noperiods + 1; > this.lesson.ColumnStyles.Add(new > System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, > 120F)); > for (int i = 0; i < noperiods; ++i) > { > this.lesson.ColumnStyles.Add(new > System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, > periodSize)); > this.periodlabel[i].Location = new System.Drawing.Point(127 + > (117 * > i), 2); > this.periodlabel[i].Text = "Lesson " + i.ToString(); > this.periodlabel[i].AutoSize = true; > this.periodlabel[i].Font = new System.Drawing.Font("Arial", 18F, > System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, > ((byte)(0))); > this.periodlabel[i].Size = new System.Drawing.Size(109, 27); > this.periodlabel[i].TabIndex = i + 6; > this.lesson.Controls.Add(this.periodlabel[i], 0, i); > } > } > > The source compiles fine (well, it tells me that Field > 'timetabler.lessonplan.periodlabel is never assigned to, and will always > have its default value null, but from what I've read, that's ok) , but > when I run the application and click on the button which instantates the > lessonplan class (it is passing a value happily, if I remove the > periodlabel code, everything works find), I get > > System.NullReferenceException: Object reference not set to an instance > of an object > > I've tried a number of solutions to this (including putting a > periodlabel[i] = new Label(); just before the addition of the > periodlabel values but within the braces, trying to create dummy > instances in the designer source file and a few other tricks found > online - nothing worked). > > Is there a nice, easy way of doing what I want here? > > TTFN > > Paul > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Mono-winforms-list maillist - Mono-winforms-list@lists.ximian.com > http://lists.ximian.com/mailman/listinfo/mono-winforms-list _______________________________________________ Mono-winforms-list maillist - Mono-winforms-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-winforms-list