Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting an error when defining a list of lists

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


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’

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading