Ben Prince

Ben Prince

  • NA
  • 9
  • 8.4k

creating a line at runtime, then using an array to create multiples?

Dec 13 2010 2:34 PM

OK, this is my first post so please be gentle.....
I am a total noob to programming in c# and have just started doing a bit for my dissertation.
I am, initially trying to create a line in a silverlight application (which I can do) using the following code:
int positionX = 12;
int positionY = 12;
int staffLength = 1500;
int staffLineNo = 6;
Brush blackBrush = new SolidColorBrush(Colors.Black);
Line staffLine = new Line();
staffLine.Stroke = blackBrush;
staffLine.X1 = positionX;
staffLine.Y1 = positionY;
staffLine.X2 = positionX + staffLength;
staffLine.Y2 = positionY;
LayoutRoot.Children.Add(staffLine);
Which works a treat!
now what I want to do is create anything from 4-12 lines on runtime depending on a variable. So, I thought I will put it in an array with this code:
int
positionX = 12;
int positionY = 12;
int staffLength = 1500;
int staffLineNo = 6;
Brush blackBrush = new SolidColorBrush(Colors.Black);
for (int i = 1; i <= staffLineNo; i++)
{
Line[] staffLine = new Line[staffLineNo];
staffLine[i].Stroke = blackBrush;
staffLine[i].X1 = positionX;
staffLine[i].Y1 = positionY;
staffLine[i].X2 = positionX + staffLength;
staffLine[i].Y2 = positionY;
LayoutRoot.Children.Add(staffLine[i]);
}
Now, there is no error during compile, but when loaded I get an error on the
staffLine[i].Stroke = blackBrush;
error: Object reference not set to an instance of an object.

Answers (9)