none
Creating Multiple Instances of a Class RRS feed

  • Question

  • Hello, for an assignment I need to create a student mark book where the user is asked to input the number of students they have, and I need to create instances (student1, student2, student3....) for each of the students, then assign properties to each of them. I tried using a for loop, but it did not work. Please just show me how I can create an instance for each student in the class, I can complete the rest of the code myself. Thanks in advance....

    P.S: My teacher has given permission to ask other sources, especially MSDN forums for help...

    Monday, November 19, 2018 2:31 AM

All replies

  • The simple answer is something like so...

    Student student1 = new Student();
    student1.Name = "Joe Bloggs";
    student1.ID = 123;
    
    Student student2 = new Student();
    student2.Name = "Fred Nurk";
    student2.ID = 456;

    But I suspect that's not what you are after, because there is a problem if the number of students is variable you can't call them student1, student2, student3 and so on without knowing in advance how many there are. So it looks like you will need an array or list of some kind, that you can fill out with as many students as needed. A 'for' loop would, in fact, serve this purpose if done correctly.

    We are not supposed to write too much of your code for you, but if you show us what you have so far (that didn't work) we might be able to point out where you went wrong.

    Monday, November 19, 2018 2:44 AM
  • Here is a pattern

    public class Student
    {
        public int id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    Usage

    var studentsList = new List<Student>()
    {
        new Student() { id = 1, FirstName = "Karen", LastName = "Payne"}
    };
    
    studentsList.Add(new Student() {id = 2, FirstName = "Jane", LastName = "Smith"});
    

    Be sure to read the following

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers

    Since this is a school assignment this is all I have for you as you need to learn, I've given you a push in the proper direction.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, November 19, 2018 2:49 AM
    Moderator
  • Hi Anonymous,

    If you want to achieve them via for loop, maybe all of the student have the similar name, like this:

    class Program
        {
            static void Main(string[] args)
            {          
                List<Student> students = new List<Student>();
    
                for (int i = 0; i < 10; i++)
                {
                    Student student = new Student();
                    student.StudentId = i;
                    student.Name = "TestName" + i;
                    if (i % 2 == 0)
                    {
                        student.Sex = "Male";
                    }
                    else
                    {
                        student.Sex = "FeMale";
                    }
    
                    students.Add(student);
                }
    
                Console.ReadLine();
            }
        }
    
        public class Student
        {
            public int StudentId { get; set; }
            public string Name { get; set; }
            public string Sex { get; set; }
        }

    So I suggest that you could create students one by one and save it them into a List. such as Karen mentioned.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 20, 2018 2:01 AM
    Moderator