0

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];
            }
julianconcepcion
  • 187
  • 3
  • 18
  • You could use a Dictionary>. MSDN reference: http://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx – Adrian Fâciu Feb 10 '14 at 15:04
  • Thank you for the fast reply but, is there an easier way like the second last code I have given in my question? It seems to be so complicated – julianconcepcion Feb 10 '14 at 15:07
  • 1
    That's exactly what @AdrianFaciu's answer would do. You need to put your 4 lists into another collection so that you can easily switch between them. – David Feb 10 '14 at 15:14
  • I got it know because of the sample code @codeCaster made, but I am still thankful for you guys! – julianconcepcion Feb 10 '14 at 15:41

2 Answers2

3

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.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • If I want to sort my first result, is this correct? result[1].OrderBy(c => c.Y).ToList(); – julianconcepcion Feb 10 '14 at 15:40
  • 1
    I know this is the best way, but I'm having a hard time using this kind of method in c#. I'm only processing 20 Points so I guess it is okay if I used @ic3b3rg's way, which is easier to understand for me. But anyway, thanks alot! – julianconcepcion Feb 10 '14 at 18:12
1

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]);
ic3b3rg
  • 14,629
  • 4
  • 30
  • 53