0

Looking for something like:

dim rect as system.drawing.rectangle

For each point in rect Debug.print(point.name, point.value) Next

2 Answers 2

1

you could write an extension method that returns you an array of the points you want. I don't really do VB, but an example in C# would be something like...

public static class RectangleExtensions
{
    public static Point[] GetPoints(this Rectangle rect)
    {
        return new Point[]
        {
            new Point(rect.Left, rect.Top),
            new Point(rect.Right, rect.Top),
            new Point(rect.Right, rect.Bottom),
            new Point(rect.Left, rect.Bottom)
        };
    }
}

public class example
{
    public void ExampleMethod()
    {
        Rectangle rect = new Rectangle();
        foreach (Point point in rect.GetPoints())
            Console.WriteLine(point.ToString());
    }
}
2
  • Thanks, I can use this but still holding out for way to do it without my own function. This is the system.drawing namespace rectangle object -- how can there be no built-in way to loop through the points?
    – pghcpa
    Sep 8, 2010 at 20:30
  • 1
    Check that yourself. There is no built in "GetPoints" method or anything. Why would there be? It isn't really a particularly common operation. Sep 9, 2010 at 4:12
1
Function GetAllPoints(ByVal r As Rectangle) As Point()
    Return { _
    New Point(r.Left, r.Top), _
    New Point(r.Right, r.Top), _
    New Point(r.Left, r.Bottom), _
    New Point(r.Right, r.Bottom) _
    }
End Function

(Line continuation added to support older versions of VB.NET)

1
  • Yes, how embarrassing. Fixed.
    – Larsenal
    Sep 7, 2010 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.