0

I am trying to define a list in a list so that I can store the following data but getting the error as defined at end.

(x = 50, y = 25)

(x = 33, y => 50) (x = 66, y = 50)


My code is as follows

// == Classes
public class XYPos
{
  public int x { get; set; }
  public int y { get; set; }
}

public class Positions : List<XYPos>{}
// == Define the data code
var positionGroups = new List<Positions>();

var positions = new List<XYPos>();
positions.Add(new XYPos { x = 50, y = 25});
positionGroups.Add(new List<Positions>(positions)); **

var positions = new List<XYPos>();
positions.Add(new XYPos { x = 33, y = 50});
positions.Add(new XYPos { x = 66, y = 50});
positionGroups.Add(new List<Positions>(positions));

I am getting this error on line ** Argument 1: cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'

derHugo
  • 83,094
  • 9
  • 75
  • 115

1 Answers1

1

I am a bit confused on why you create a class which is a specific list of other classes, but I think you're looking for something along these lines:

var positions = new List<XYPos>();
positions.Add(new XYPos { x = 33, y = 50 });
positions.Add(new XYPos { x = 66, y = 50 });

var positionGroups = new List<List<XYPos>>();
positionGroups.Add(positions);

The "groups" is a list of lists.. So there are multiple (grouped) XY positions within that list. If you're only looking for a group of XY positions, you have no need for the public class Positions : List<XYPos>{} at all.

Also worth to mention, if you make a list of lists (without any specific identifier on which list that group is about) you might have a lot of looping to do to find a specific item.

riffnl
  • 3,248
  • 1
  • 19
  • 32
  • Thank you I can now see what I was doing wrong. Thanks for the comment about not having an identifier. I will be ok as I do actually need to loop through all of the set. – David Rouse Oct 11 '22 at 19:44