I have a website with a table of rows. Each row has two columns of values. what I want to do is to store each row into own place in a list, and have user the ability to enter the value in the list and access each column value individually, i.e; not just making a string of the row. The final code will contain a for loop to loop through the rows and webscraping code, but for troubleshooting I omitted that and am just hardcoding the values. I began with 2 classes, one for the getter/setters for the individual class values and another to get/set that object. From there I created a method to set the values.

        public class Student
        {
            public string Name { get; set; }
            public string Address { get; set; }
        }

        public class ListOfStudents
        {
            public Student student { get; set; }
        }

        public static ListOfStudents GetListOfStudents()
        {
            List<Student> students = new List<Student>();
            Student student = new Student
            {
                Name = "Mike",
                Address = "Main St"
            };            
            ListOfStudents listOfStudents = new ListOfStudents
            {
                students.Add(Student)
            };
            return listOfStudents;
        }

I'm getting this error "Cannot initialize type 'ListClass.ListOfStudents' with a collection initializer because it does not implement System.Collections.IEnumerable" Essentially I can't determine how to make the list of objects.

2 Answers

1
evilGenius On Best Solutions

You forgot use List

public class ListOfStudents
{
    public List<Student> Students { get; set; }
}

and other code:

List<Student> students = new List<Student>();
Student student = new Student
{
    Name = "Mike",
    Address = "Main St"
}; 
students.Add(Student);           
ListOfStudents listOfStudents = new ListOfStudents
{
    Students = students;
};

Edit

But your code very wierd, pls say what you really want

0
sayah imad On

There is an error in your code you because students is not a propertie of ListOfStudents Without forgetting that the student propertie of ListOfStudents suppose to be a list you can use the code below

    public class Student
    {
        public string Name { get; set; }
        public string Address { get; set; }
    }

    public class ListOfStudents
    {
        public List<Student> Students { get; set; }
    }

    public static ListOfStudents GetListOfStudents()
    {
        List<Student> students = new List<Student>();
        Student student = new Student
        {
            Name = "Mike",
            Address = "Main St"
        };
        students.Add(student);
        ListOfStudents listOfStudents = new ListOfStudents()
        {
            Students = students
        };
        return listOfStudents;
    }

As a .net developer in the current context I recommend to use what is called an Indexer

Indexer :

An indexer is a special type of property that provides access to a class or structure in the same way as a table for its internal collection. This is the same as the property except that it has been defined with this keyword with square brackets and parameters.

[source] https://www.tutorialsteacher.com/csharp/csharp-indexer

Exemple of how to use an indexer in your context :

 public class Student
 {
    public string Name { get; set; }
    public string Address { get; set; }
 }

 public class ListOfStudents
 {
    public Student[] Students = new Student[100];

    public Student this[int index]
    {
        get { return Students[index]; }
        set { Students[index] = value; }
    }
 }