Home > Net >  Getting an error when defining a list of lists
Getting an error when defining a list of lists

Time:10-12

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'

CodePudding user response:

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.

  • Related