Question

I will first show you my code because I'm having a hard time finding words to explain what I want, I have a List of Points which contains coordinatesList<PointF> points;, they are 20 coordinates to be exact. I want to separate those 20 points into 4 List of Points. This are my 4 List of Points:

        List<PointF> Col1 = new List<PointF>();
        List<PointF> Col2 = new List<PointF>();
        List<PointF> Col3 = new List<PointF>();
        List<PointF> Col4 = new List<PointF>();

This is my code :

        int loop = 1;
        while (loop <= 20) 
        {
            var identifier = 1;
            if (loop % 5 == 0)
            {
                identifier++;
            }
            else 
            {//Here is what I'm talking about, if I want it to be like Col1 + identifier.ToString(), something like that
                Col.Add(new PointF(points.ElementAt(loop - 1).X, points.ElementAt(loop - 1).Y));   
            }
        }

What i want to do is if my loop is already at 5 I want my Col1.add to be "Col2.add" and if my loop is equal to 10, I want my Col2.add to be "Col3.add" and if my loop is equal to 15, I want my Col3.add to be "Col4.add". I dont know how to say this but, I want to increment my List names.

I want something like this, but it is with 20 PictureBox and not with variables.

                for (int x = 1; x <= ExtractedBoxes.Count(); x++)
            {
                ((PictureBox)this.Controls["pictureBox" + x.ToString()]).Image = ExtractedBoxes[x - 1];
            }
Was it helpful?

Solution 2

An array may be a better solution:

List<PointF>[] lists = {new List<PointF>(), new List<PointF>(), new List<PointF>(), new List<PointF>()};

for(int i=0;i<20;i++) lists[i/5].Add(points[i]);

OTHER TIPS

You can do it with a Dictionary<int, List<PointF>> as suggested by @Adrian:

var result = new Dictionary<int, List<PointF>>();
int identifier = 0;
result[identifier] = new List<PointF>();

for (int loop = 0; loop < 19; loop++) 
{
    if ((loop - 1) % 5 == 0)
    {
        result[++identifier] = new List<PointF>();
    }

    result[identifier].Add(points[loop]);       
}

See Split List into Sublists with LINQ for an alternative.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top