I am trying to model a field boundary which is a polygon made up of lines which are defined by their end points which are x,y coordinates.
I am all right writing objects for points and lines but when I try to model the polygon as an 'ArrayList' of lines I can't use 'foreach' to access it. I want to use 'ArrayList as I don't know how many sides the polygon will have.
Quote:
|
quote:foreach statement cannot operate on variables of type 'RoutePlanner.Polygon' because 'RoutePlanner.Polygon' does not contain a public definition for 'GetEnumerator'
|
Code:
public class Polygon
{
private ArrayList lines; // A polygon is an array of Lines
public Polygon(int sides) // Constructor. Parameter is number of lines
{
lines = new ArrayList(sides);
}
public Polygon()
{
lines = new ArrayList();// Constructor. null arraylist
}
public void Add(Line newLine)
{
lines.Add(newLine);
}
}
Code:
private void btnTest_Click(object sender, EventArgs e)
{
Polygon myBoundary = new Polygon();
Point MyPoint1 = new Point(2, 4);
Point MyPoint2 = new Point(6, 8);
Line MyLine1 = new Line(MyPoint1, MyPoint2);
myBoundary.Add(MyLine1);
Point MyPoint3 = new Point(12, 14);
Point MyPoint4 = new Point(16, 18);
Line MyLine2 = new Line(MyPoint3, MyPoint4);
myBoundary.Add(MyLine2);
foreach (Line myLine in myBoundary)
{
string MyString = string.Format("From Line x1= {0} y1= {1} x2= {2} y2= {3}",
myLine.From.X, myLine.From.Y, myLine.To.X, myLine.To.Y);
MessageBox.Show(MyString);
}
}
Regards
Colin