Populating a nest list in class

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
28,048
Reaction score
17,800
I've actually never done this before, populating a sub list in a class. I'm getting a weird error and I have no idea how to resolve. Please what I must do, any suggestions?

C#:
    public class Student
    {
        public string StudentNo { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public char Gender { get; set; }
        public DateTime Dob { get; set; }
        public List<Marks> marksList { get; set; }

    }

    public class Marks
    {
        public string StudentNo { get; set; }
        public string SubjectName { get; set; }
        public string SubjectMark { get; set; }
    }

C#:
            List<Student> students = new List<Student>
            {
                 new Student(){StudentNo = "1", Name="Johnny",Surname="Walker",Gender='M', Dob=Convert.ToDateTime("2008-12-01"), marksList = new List<Marks> { marks } },
            };

sublist.jpg
 
I've actually never done this before, populating a sub list in a class. I'm getting a weird error and I have no idea how to resolve. Please what I must do, any suggestions?

C#:
    public class Student
    {
        public string StudentNo { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public char Gender { get; set; }
        public DateTime Dob { get; set; }
        public List<Marks> marksList { get; set; }

    }

    public class Marks
    {
        public string StudentNo { get; set; }
        public string SubjectName { get; set; }
        public string SubjectMark { get; set; }
    }

C#:
            List<Student> students = new List<Student>
            {
                 new Student(){StudentNo = "1", Name="Johnny",Surname="Walker",Gender='M', Dob=Convert.ToDateTime("2008-12-01"), marksList = new List<Marks> { marks } },
            };

sublist.jpg
Your code sample is incomplete, but form the error your ‘marks’ is already a list, therefore it should be
Code:
marksList = marks;

or

Code:
marksList = new List<Marks>(marks)

List initializer is suger for .Add and that is why you cannot use it.

FYI a better naming convention is class name “Mark” and property name “Marks”.

“MarksList” is like calling a property “FirstNameString”
 
Last edited:
Your code sample is incomplete, but form the error your ‘marks’ is already a list, therefore it should be
Code:
marksList = marks;

or

Code:
marksList = new List<Marks>(marks)

List initializer is suger for .Add and that is why you cannot use it.

FYI a better naming convention is class name “Mark” and property name “Marks”.

“MarksList” is like calling a property “FirstNameString”
Would still be fine with markList, says what it is, but yeah, marks would be better as add s for collection if it works in English.

Each is a singular mark based on that class (as it should be), would also capitalize the Mark list, since publicly accessible.
 
Code:
List<Student> students = new List<Student>
{
    new Student(){StudentNo = "1", Name="Johnny",Surname="Walker",Gender='M', Dob=Convert.ToDateTime("2008-12-01"), marksList = (new List<List<Marks>> { marks }).SelectMany(x => x) },
};
o_Oo_Oo_O:cool:
 
Code:
List<Student> students = new List<Student>
{
    new Student(){StudentNo = "1", Name="Johnny",Surname="Walker",Gender='M', Dob=Convert.ToDateTime("2008-12-01"), marksList = (new List<List<Marks>> { marks }).SelectMany(x => x) },
};
o_Oo_Oo_O:cool:

Because Johnny Walker :X3:

Busy making a web service out of that app :p
 
I'm actually getting a list within a list if I do it this way.

When I return a student, I get a list of marks, then I open that I have another list of marks, only when I open that do I actually see the marks :oops:

But you put the marks list within another marks list. So the code is working as expected.

Take it step by step. Which part is knocking you out? ... :

C#:
List<Marks> marksList = new List<Marks> (); //Creates new marks list

Marks mark1 = {StudentNo="1",SubjectName = "History",SubjectMark = "50"} //Create a mark
Marks mark2 = {StudentNo="1",SubjectName = "Maths",SubjectMark = "70"} //Create another mark

marksList.Add(mark1); //Add the first mark to the list
marksList.Add(mark2); //Add the second mark to the list

//Add the list of marks to the right student
List<Student> students = new List<Student>
{
    new Student(){StudentNo = "1", Name="Johnny",Surname="Walker",Gender='M', Dob=Convert.ToDateTime("2008-12-01"), markslist },
};
 
Last edited:
It's working. Was just me being pedantic about how the debugging looked when inspected. It works perfectly.
 
Top
Sign up to the MyBroadband newsletter
X