1

我正在做一个学校项目。用户可以输入 Persons 对象并将它们存储在 Person 类型的 List 中。它工作正常,期望当我从另一个类调用列表时,列表是空的!

class SubMenu
{
    CreatePersons cp;
    List<Person> myList = new List<Person>();
    public List<Person> MyList
    {
        get { return myList; }
    }

    string UserInput= null;
    public void SMenu()
    {

        Boolean flag = true;

        while (true)
        {
            Console.WriteLine("");
            Console.WriteLine(" -------------------------------------------");
            Console.WriteLine(" -|---------------------------------------|-");
            Console.WriteLine(" -|- P. Create a Person                  -|-");
            Console.WriteLine(" -|- S. Create a Student                 -|-");
            Console.WriteLine(" -|- T. Create a Teacher                 -|-");
            Console.WriteLine(" -|- A. Create Administrative Staff      -|-");
            Console.WriteLine(" -|- R. Return to Main Menu              -|-");
            Console.WriteLine(" -|---------------------------------------|-");
            Console.WriteLine(" -------------------------------------------");
            Console.Write(" Enter your choice: ");
            UserInput = Console.ReadLine();

            switch (UserInput.ToLower())
            {
                case "p": myList.Add(new Person("Johnny"));   //For testing
                          Console.WriteLine(myList.Count);    //For testing .. here count = 1 as it should be.
                          break;
                case "s": Console.WriteLine("Creates and object of type Student.");

                    break;

这是用户创建新人员的类。我创建了一个 Person 进行测试。问题是当我从另一个类调用列表时,列表是空的,它应该有一个同伴!这是另一个类的代码。

 class MainMenu
    {
         Boolean flag = true;
         SubMenu sm = new SubMenu();
         public void MMenu()
         {
             while (true)
             {
                 Console.WriteLine();
                 Console.WriteLine("Console Application v1.0 created by");
                 Console.WriteLine();
                 Console.WriteLine(" ---------------------------------------------------");
                 Console.WriteLine(" -|-------------- MENU ---------------------------|-");
                 Console.WriteLine(" -|-----------------------------------------------|-");
                 Console.WriteLine(" -|- 1 -  Create a Person                        -|-");
                 Console.WriteLine(" -|- 2 -  List all the Persons                   -|-");
                 Console.WriteLine(" -|- 3 -  Search for a Person                    -|-");
                 Console.WriteLine(" -|- 4 -  Display the number of Persons          -|-");
                 Console.WriteLine(" -|- 5 -  Exit                                   -|-");
                 Console.WriteLine(" ---------------------------------------------------");
                 Console.Write(" Enter your choice: ");
                 string UserInput = Console.ReadLine();
                 Console.WriteLine();

                 int input;
                 if (int.TryParse(UserInput, out input))
                 {
                     switch (input)
                     {
                         case 1:Console.WriteLine("Case 1");
                                sm.SMenu();
                                break;
                         case 2:Console.WriteLine("Case 2");  //This should display the one, the current size of the List.
                                int size = sm.MyList.Count;
                                Console.WriteLine("Size :" + size);

                                break;
                         case 3: Console.WriteLine("Case 3");

请查看评论,以便可以轻松识别问题。

谢谢你的帮助。

4

1 回答 1

0

我不确定如何调用SMenuwihout 将变量设置为static,因此我尝试对您的代码进行一些更改,并且看起来效果很好....如果您想保留之前所做的值,那么您需要静态的。

我删除了,Person因为我不知道它的定义。

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

namespace ConsoleApplication5
{
    class Program
    {
        Boolean flag = true;
        //I can only run it after I set it to static
        static SubMenu sm = new SubMenu();
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine();
                Console.WriteLine("Console Application v1.0 created by");
                Console.WriteLine();
                Console.WriteLine(" ---------------------------------------------------");
                Console.WriteLine(" -|-------------- MENU ---------------------------|-");
                Console.WriteLine(" -|-----------------------------------------------|-");
                Console.WriteLine(" -|- 1 -  Create a Person                        -|-");
                Console.WriteLine(" -|- 2 -  List all the Persons                   -|-");
                Console.WriteLine(" -|- 3 -  Search for a Person                    -|-");
                Console.WriteLine(" -|- 4 -  Display the number of Persons          -|-");
                Console.WriteLine(" -|- 5 -  Exit                                   -|-");
                Console.WriteLine(" ---------------------------------------------------");
                Console.Write(" Enter your choice: ");
                string UserInput = Console.ReadLine();
                Console.WriteLine();

                int input;
                if (int.TryParse(UserInput, out input))
                {
                    switch (input)
                    {
                        case 1: Console.WriteLine("Case 1");
                            sm.SMenu();
                            break;
                        case 2: Console.WriteLine("Case 2");  //displaying one now if run once
                            int size = sm.MyList.Count;
                            Console.WriteLine("Size :" + size);

                            break;
                        case 3: Console.WriteLine("Case 3");
                            break;
                    }
                }
            }
        }

        class SubMenu
        {
            //CreatePersons cp;
            //replace Person with int
            List<int> myList = new List<int>();
            public List<int> MyList
            {
                get { return myList; }
            }

            string UserInput = null;
            public void SMenu()
            {

                Boolean flag = true;
                //the original while(true) makes me never go out from loop, so I change it.
                do
                {
                    Console.WriteLine("");
                    Console.WriteLine(" -------------------------------------------");
                    Console.WriteLine(" -|---------------------------------------|-");
                    Console.WriteLine(" -|- P. Create a Person                  -|-");
                    Console.WriteLine(" -|- S. Create a Student                 -|-");
                    Console.WriteLine(" -|- T. Create a Teacher                 -|-");
                    Console.WriteLine(" -|- A. Create Administrative Staff      -|-");
                    Console.WriteLine(" -|- R. Return to Main Menu              -|-");
                    Console.WriteLine(" -|---------------------------------------|-");
                    Console.WriteLine(" -------------------------------------------");
                    Console.Write(" Enter your choice: ");
                    UserInput = Console.ReadLine();

                    switch (UserInput.ToLower())
                    {
                        //remove the Person part
                        case "p": myList.Add(1);   //For testing
                            Console.WriteLine(myList.Count);    //Count = 1
                            break;
                        case "s": Console.WriteLine("Creates and object of type Student.");
                            break;
                    }
                } while (false);
            }
        }
    }
}
于 2014-03-28T17:32:05.423 回答