2013-10-14 59 views
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; 

這是用戶創建新人員的類。我創建了一個人進行測試。問題是,當我從另一個班級調用列表時,該列表是空的,它應該有一個peeron! 以下是其他課程的代碼。

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"); 

請看看評論,這樣的問題可以很容易理解。

感謝您的幫助。

+2

你正在執行情況1在MMenu方法的情況2之前,對嗎?否則,SMenu尚未被調用,所以列表應該是空的。 – FishBasketGordo

+0

您在這裏使用菜單元素的順序是相關的,所以您可以詳細說明您使用菜單的順序。另外,是否有執行switch語句之外的任何代碼? – Kami

+0

在我執行案例2之前,我正在使用MMenu中的案例1創建一個人,然後去SMenu。但是當我回到MMenu時,這個列表是空的。 – user2307236

回答

0

我不知道你怎麼能叫SMenu wihout設置變量爲靜態,所以我儘量讓你的代碼的一些變化,看起來像它運作良好 ....如果你想儲備你之前所做的價值,那麼你需要靜態。因爲我不知道它的定義,我刪除了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); 
      } 
     } 
    } 
}