包含类对象的列表不起作用

时间:2014-11-04 03:44:47

标签: c# list class

我正在开发一个小型C#程序,它将学生记录存储在一个列表中。我需要使用一个类来保存每个学生记录,这是我下面的代码示例中的顶级课程。

如果用户选择创建新记录,我会将其输入放入studentRecord变量,然后将其添加到recordList。但是,当我尝试使用Count()显示当前可用的学生记录数时,程序除了重新显示菜单外什么都不做,好像完全忽略了我要显示的命令。我认为我声明列表的方式/位置或者每次运行菜单时如何创建新的StudentRecord对象都有问题。

此外,所有三种方法都必须留在第一堂课。有什么方法可以解决这个问题吗?

public class StudentRecord
{
    //Declare the various fields of the class
    private string strFirstName;
    private string strLastName;
    private int intCourses;
    private int intCreditHours;

    List<StudentRecord> lstRecords = new List<StudentRecord>();

    //Declare the properties of the class, since the fields are private
    public string StrFirstName
    {
        get
        {
            return strFirstName;
        }
        set
        {
            strFirstName = value;
        }
    }

    public string StrLastName
    {
        get
        {
            return strLastName;
        }
        set
        {
            strLastName = value;
        }
    }

    public int IntCourses
    {
        get
        {
            return intCourses;
        }
        set
        {
            intCourses = value;
        }
    }

    public int IntCreditHours
    {
        get
        {
            return intCreditHours;
        }
        set
        {
            intCreditHours = value;
        }
    }


    //Declare a default constructor
    public StudentRecord()
    {            
    }

    //Declare a constructor that takes the four necessary parameters, and set the class
    //  properties equal to the respective parameters
    public StudentRecord(string firstName, string lastName, int courses, int creditHours)
    {
        strFirstName = firstName;
        strLastName = lastName;
        intCourses = courses;
        intCreditHours = creditHours;
    }


    //Declare a method to perform the adding a student record function
    public void mtdAddStudentRecord()
    {
        //These variables temporarily hold the various user inputs
        string strInputFirstName;
        string strInputLastName;
        int intInputCourses;
        int intInputCreditHours;


        //Prompt the user to enter the student's first name
        Console.Write("Please enter the first name:    ");
        strInputFirstName = Console.ReadLine();


        //Prompt the user to enter the student's last name
        Console.Write("Please enter the last name:    ");
        strInputLastName = Console.ReadLine();

        //Prompt the user to enter the student's number of courses
        Console.Write("Please enter the number of courses:    ");
        intInputCourses = int.Parse(Console.ReadLine());

        //Prompt the user to enter the student's completed credit hours
        Console.Write("Please enter the number of completed credit hours:    ");
        intInputCreditHours = int.Parse(Console.ReadLine());


        //Add the new student record to the list, using the paramaters of the second
        //  class constructor
        lstRecords.Add(new StudentRecord(strInputFirstName, strInputLastName,
            intInputCourses, intInputCreditHours));
    }


    //Declare a method to perform the display student information option
    public void mtdDisplayStudentInformation()
    {
        Console.WriteLine("Capacity: {0}", lstRecords.Count);
    }


    //Declare a method to perform the edit student information option
    public void mtdEditStudentInformation()
    {
        //TODO
    }

}




public class Program
{
    public static void Main(string[] args)
    {
        //Declare and initialize a variable to store the user menu choice
        string strMenuChoice;
        strMenuChoice = "";


        //Perform the necessary menu option while the user has NOT chosen to exit
        do
        {
            //Call the reset method to clear the screen and display the header
            mtdResetConsole();

            //Display a menu to the user
            Console.Write("Please choose an option to perform:" +
                "\n   - A) Display a list of existing students" +
                "\n   - B) Add a new student record" +
                "\n   - C) Edit an existing student record" +
                "\n   - D) Exit the program" + "\n\n" + "     ");

            //Store the user reply
            strMenuChoice = Console.ReadLine().ToLower();


            //Create a new StudentRecord object, and use it to call the various methods
            //  as chosen by the user
            StudentRecord studentRecord = new StudentRecord();

            //Determine which option was chosen, and take the appropriate action
            switch (strMenuChoice)
            {
                case "a":
                    //TODO - Necessary code for option A
                    mtdResetConsole();

                    //Perform the "view student records" method
                    studentRecord.mtdDisplayStudentInformation();

                    break;

                case "b":
                    //TODO - Necessary code for option B
                    mtdResetConsole();

                    //Perform the "add student record" method
                    studentRecord.mtdAddStudentRecord();                        

                    break;

                case "c":
                    //TODO - Necessary code for option C
                    break;

                case "d":
                    //Exit the program
                    Environment.Exit(0);
                    break;

                default:
                    mtdResetConsole();
                    Console.WriteLine("Error" + "\n" +
                        "   - Please choose a valid option from the list");

                    //Pause the code from executing for 2.5 seconds, so that the error
                    //  message will be displayed
                    System.Threading.Thread.Sleep(2500);

                    break;
            }
        }
        while (strMenuChoice.ToLower() != "d");
    }



    //Declare a method to reset the console with a blank screen and header
    public static void mtdResetConsole()
    {
        Console.Clear();
        Console.WriteLine("CONESTOGA STUDENT RECORDS" + "\n");
    }
}

2 个答案:

答案 0 :(得分:0)

首先,把这个放在外面做

    StudentRecord studentRecord = new StudentRecord();

将此内置放入do意味着对象将在每个循环中重置。

2,尝试在mtdDisplayStudentInformation方法上放置类似Console.ReadLine()的东西来保持屏幕。

    public void mtdDisplayStudentInformation()
    {
        Console.WriteLine("Capacity: {0}", lstRecords.Count);
        Console.ReadLine();
    }

答案 1 :(得分:0)

Kendall,尝试将“记录”和“记录列表”的逻辑分开。 在下面的情况下,您为每个创建的记录都有一个“记录列表”。

建议您创建一个静态变量,以确保它在所有“记录”中始终是相同的列表,或者您将其分开:

public class StudentRecord
{
     //Declare the various fields of the class
    private string strFirstName;
    private string strLastName;
    private int intCourses;
    private int intCreditHours;
...
}

public class Program
{
     private List<StudentRecord> records = new List<StudentRecord>();
     public static void Main(string[] args)
     {
        ...
     }
}