# How would I go about adding multiple rectangles using the same lines of code? • ### Question

• How would I go about adding multiple rectangles using the same lines of code? I would prefer to just run through a set of code every time I need a polygon. If I just have to create multiple polygon adding statements that's fine but I'd prefer just 1.
Tuesday, April 21, 2015 1:24 PM

• >>How would I go about adding multiple rectangles using the same lines of code?

You could create a method that creates and returns x number of Rectangle elements:

```        public IEnumerable<Rectangle> CreateRectangles(int numberOfRectsToCreate)
{
for (int i = 0; i < numberOfRectsToCreate; ++i)
{
Rectangle rect = new Rectangle();
rect.Fill = Brushes.Blue;
rect.Width = 100;
rect.Height = 100;
yield return rect;
}
}```

..and then call this method from anywhere in your code:

```            IEnumerable<Rectangle> rects = CreateRectangles(5);
foreach (Rectangle rect in rects)
{
//add to StackPanel or do whatever with the Rectangle elements:
}```

>>If I just have to create multiple polygon adding statements that's fine but I'd prefer just 1.

When adding Point objects to a Polygon you can only add one per call to the Add method but you could call the Add method inside a loop, e.g:

```            for(int i = 0; i < 10; ++i)
{
//add to StackPanel or do whatever with the Rectangle elements:
Polygon p = new Polygon();

}```

• Proposed as answer by Wednesday, April 22, 2015 8:00 AM
• Marked as answer by Wednesday, April 29, 2015 8:17 AM
Tuesday, April 21, 2015 1:46 PM
• The best thing you could do here is to add the Point objects to a collection and then iterate through this collection:

```if (picToggle == 2)
{
point3.X = point2.X;
point4.X = point5.X;
point7.X = point6.X;
point8.X = point1.X;
point4.Y = wire5.Y;
point7.Y = wire6.Y;
point3.Y = wire5.Y;
point8.Y = wire6.Y;
PointCollection blop = new PointCollection();
List<Point> thePoints = new List<Point> { point1, point2, point3, point4, point5, point6, point7, point8 };
foreach (var point in thePoints)
plop.Stroke = new SolidColorBrush(Colors.Black);
plop.Fill = new SolidColorBrush(Colors.Yellow);
plop.Points = blop;
LastImageClicked = null;
}```

It will save you a few lines of codes if that's what you want.

• Marked as answer by Wednesday, April 29, 2015 8:17 AM
Monday, April 27, 2015 8:58 PM

### All replies

• >>How would I go about adding multiple rectangles using the same lines of code?

You could create a method that creates and returns x number of Rectangle elements:

```        public IEnumerable<Rectangle> CreateRectangles(int numberOfRectsToCreate)
{
for (int i = 0; i < numberOfRectsToCreate; ++i)
{
Rectangle rect = new Rectangle();
rect.Fill = Brushes.Blue;
rect.Width = 100;
rect.Height = 100;
yield return rect;
}
}```

..and then call this method from anywhere in your code:

```            IEnumerable<Rectangle> rects = CreateRectangles(5);
foreach (Rectangle rect in rects)
{
//add to StackPanel or do whatever with the Rectangle elements:
}```

>>If I just have to create multiple polygon adding statements that's fine but I'd prefer just 1.

When adding Point objects to a Polygon you can only add one per call to the Add method but you could call the Add method inside a loop, e.g:

```            for(int i = 0; i < 10; ++i)
{
//add to StackPanel or do whatever with the Rectangle elements:
Polygon p = new Polygon();

}```

• Proposed as answer by Wednesday, April 22, 2015 8:00 AM
• Marked as answer by Wednesday, April 29, 2015 8:17 AM
Tuesday, April 21, 2015 1:46 PM
• So how could I take this code and put in a loop that only loops once per call?

```if (a == 6 && LastImageClicked != null)
{
if (picToggle == 2)
{
point3.X = point2.X;
point4.X = point5.X;
point7.X = point6.X;
point8.X = point1.X;
point4.Y = wire5.Y;
point7.Y = wire6.Y;
point3.Y = wire5.Y;
point8.Y = wire6.Y;
PointCollection blop = new PointCollection();
plop.Stroke = new SolidColorBrush(Colors.Black);
plop.Fill = new SolidColorBrush(Colors.Yellow);
plop.Points = blop;
LastImageClicked = null;
}
}```

Monday, April 27, 2015 5:58 PM
• "

So how could I take this code and put in a loop that only loops once per call?

"

That's a trick question, right?

Because it's already going to only run once.

Hope that helps.

Technet articles: Uneventful MVVM; All my Technet Articles

Monday, April 27, 2015 6:05 PM
• The best thing you could do here is to add the Point objects to a collection and then iterate through this collection:

```if (picToggle == 2)
{
point3.X = point2.X;
point4.X = point5.X;
point7.X = point6.X;
point8.X = point1.X;
point4.Y = wire5.Y;
point7.Y = wire6.Y;
point3.Y = wire5.Y;
point8.Y = wire6.Y;
PointCollection blop = new PointCollection();
List<Point> thePoints = new List<Point> { point1, point2, point3, point4, point5, point6, point7, point8 };
foreach (var point in thePoints)
plop.Stroke = new SolidColorBrush(Colors.Black);
plop.Fill = new SolidColorBrush(Colors.Yellow);
plop.Points = blop;
• 