IcyPopTarts IcyPopTarts - 3 years ago 128
C# Question

Iterate Array Of Points

I am needing to iterate an array of points, but I am getting a compile error of:


Operator '<' cannot be applied to operands of type 'int' and 'method group' WindowsFormsApplication1


This is my syntax, how should I re-write this so that I can iterate the array? (I am only showing one Point in the example, but in my real code there are about 20)

Point[] points = new Point[] { new Point { X = 50, Y = 40 } } };

for (int i=0;i<points[i].Count;i++)
{
MessageBox.Show(points[i].X);
MessageBox.Show(points[i].Y);
}

Answer Source

your syntax for the second expression in the for loop is incorrect. But really, a foreach makes more sense here:

Point[] points = new Point[] { new Point { X = 50, Y = 40 } } };

foreach(var point in points)
{ 
    MessageBox.Show(point.X);
    MessageBox.Show(point.Y);     
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download