2
Answers

Problem with adding values to a Generic List

Iraklis Markelis

Iraklis Markelis

9y
740
1
Hi there,

Can you please help me? I'm trying to add values to a list I've made and print them to screen. Unfortunately something seems to be wrong on my code. Forgive me if I'm totally wrong but I need some help over here. Thank you very much in advance for your help.

My code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace genericTypes
{

    class Student
    {
        private string id;
        private string FName;
        private string LName;
        private string ParentID;
        private string Grade;
        private int Group;

        public Student(string id, string FName, string LName, string ParentID, string Grade, int Group)
        {
            // TODO: Complete member initialization
            this.id = ID;
            this.FName = fName;
            this.LName = lName;
            this.ParentID = parentID;
            this.Grade = grade;
            this.Group = group;
        }
        public string ID { get; set; }
        public string fName { get; set; }
        public string lName { get; set; }
        public string parentID { get; set; }
        public string grade { get; set; }
        public int group { get; set; }
    }

    /*  class Parent
    {
        public string ID { get; set; }
        public string fName { get; set; }
        public string lName { get; set; }
        public string job { get; set; }
    }   */

    class Program
    {
        static void Main(string[] args)
        {
            bool finish = true;
            string id = "0";
            string FName = "";
            string LName = "";
            string ParentID = "";
            string Grade = "";
            int Group = 0, stdNumber = 0;
            //char answer = 'Y';

            List<Student> myStudentList = new List<Student>();

            do
            {
                Console.WriteLine("Insert student's personal info");
                Console.Write("Student ID: ");
                id = Console.ReadLine();
                Console.Write("First name: ");
                FName = Console.ReadLine();
                Console.Write("Last name: ");
                LName = Console.ReadLine();
                Console.Write("Parent ID: ");
                ParentID = Console.ReadLine();
                Console.Write("Grade: ");
                Grade = Console.ReadLine();
                Console.Write("Group: ");
                Group = int.Parse(Console.ReadLine());

                myStudentList.Add(new Student(id, FName, LName, ParentID, Grade, Group));

                Console.Write("Do you want to continue input? (Y/N): ");
                ConsoleKeyInfo answer = Console.ReadKey();
                Console.WriteLine();
                finish = (answer.Key == ConsoleKey.Y) ? false : true;
            } while (finish == false);

            foreach (Student std in myStudentList)
            {
                stdNumber++;
                Console.WriteLine("Info for student number {0}", stdNumber.ToString());
                Console.WriteLine("Student ID: {0}", std.ID);
                Console.WriteLine("First name: {0}", std.fName);
                Console.WriteLine("Last name: {0}", std.lName);
                Console.WriteLine("Parent ID: {0}", std.parentID);
                Console.WriteLine("Grade: {0}", std.grade);
                Console.WriteLine("Group: {0}", std.group.ToString());
                Console.WriteLine("*******************************");
            }
        }
    }
}



Answers (2)