我正在做一个学校项目。用户可以输入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");
请查看评论,以便轻松确定问题。
感谢您的帮助。
答案 0 :(得分:0)
我不确定如何调用SMenu
并将变量设置为 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);
}
}
}
}