在类之间传递列表
本文关键字:列表 之间 | 更新日期: 2023-09-27 18:35:12
我正在做一个学校项目。用户可以输入 Person 对象并将其存储在 Person 类型的列表中。它工作正常,预计当我从另一个类调用列表时,列表为空!
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;
这是用户创建新人员的类。我创建了一个人进行测试。问题是,当我从另一个类调用列表时,列表是空的,它应该有一个peerson!这是来自另一个类的代码。
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");
请查看评论,以便轻松构思问题。
感谢您的帮助。
我不确定如何在将变量设置为静态的情况下调用SMenu
,所以我尝试对您的代码进行一些更改,看起来它运行良好......如果你想保留你之前做过的值,那么你需要静态的。
我删除了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);
}
}
}
}