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.
Loading
Ben PrincePosted Dec 30, 2010, 11:47 AM
figured it out:
//Adding the arrays
Line[] staffLine = new Line[staffLineNo];
//Entering the for loop
for (int i = 0; i < staffLineNo; i++)
{
//Adding the vertical staff lines
staffLine[i] = new Line();
positionY1 = positionY1 + staffHeight;
positionY2 = positionY2 + staffHeight;
staffLine[i].Stroke = blackBrush;
staffLine[i].X1 = positionX1;
staffLine[i].Y1 = staffHeight + positionY1;
staffLine[i].X2 = positionX2;
staffLine[i].Y2 = staffHeight + positionY2;
staffLine[i].StrokeThickness = 1;
LayoutRoot.Children.Add(staffLine[i]);
}
Now just got to work out how to change some of these settings when I click a button or something.
Sam HobbsPosted Dec 30, 2010, 7:17 AM
Ben PrincePosted Dec 30, 2010, 5:36 AM
I do understand the basics and that your suggestion puts it in an array, which is what I tried origionally with the [i] as the array could vary in size from 6 to 12 at runtime, but when I try this I get sytax errors which is why I came on here looking for a more elegant solution (which you provided) but would now like to reference each line created at runtime.
Thanks
Ben
Sam HobbsPosted Dec 19, 2010, 9:03 AM
However please, please get a book that will help you with fundamentals such as that.
Ben PrincePosted Dec 19, 2010, 8:57 AM
Sam HobbsPosted Dec 15, 2010, 8:53 PM
Sam HobbsPosted Dec 15, 2010, 8:45 PM
Also move:
Out of (before) the for loop. You don't want to create staffLine for each iteration. I don't know what the compiler will do with that line; I am surprised it did not give an error.
Also I think your for statement should be:
Ben PrincePosted Dec 15, 2010, 4:05 PM
Sam HobbsPosted Dec 13, 2010, 10:39 PM