[英]passing class object into list
I created a class with passenger properties to get details from user我创建了一个带有乘客属性的类来从用户那里获取详细信息
string trainno;
string trainname;
string pname;
string from;
string to;
then I created methods to get details然后我创建了获取详细信息的方法
public void getDetails()
{
Console.WriteLine("Enter the Trainno ");
trainno = Console.ReadLine();
Console.WriteLine("Enter the Train name ");
trainname = Console.ReadLine();
Console.WriteLine("Enter the passenger name ");
pname = Console.ReadLine();
Console.WriteLine("From : ");
from = Console.ReadLine();
Console.WriteLine("To: ");
to = Console.ReadLine();
}
and main function和主要功能
public static void Main(String[] args)
{
Class1 tr = new Class1();
storeDetails sd = new storeDetails();
tr.getDetails();
sd.store(tr);
Console.ReadLine();
}
and another class for list和另一个列表类
class storeDetails
{
List<Class1> details = new List<Class1> { };
public void store(Class1 c)
{
details.Add(c);
}
}
and I want to store details of more than one user.我想存储多个用户的详细信息。 one user details is the one element in list.一个用户详细信息是列表中的一个元素。
If you want to put the users into a list, create a loop and push to it如果要将用户放入列表,请创建一个循环并推送到它
public static void Main(String[] args)
{
Console.WriteLine("Enter number users:");
int nbUsers = int.Parse(Console.ReadLine());
storeDetails sd = new storeDetails();
for(int i = 0 ; i < nbUsers ; i++)
{
Class1 tr = new Class1();
tr.getDetails();
sd.store(tr);
}
Console.ReadLine();
}
try this尝试这个
public static void Main(String[] args)
{
Class1 tr;
Console.WriteLine("Enter number users:");
int nbUsers = Console.ReadLine();
List<Class1> sd = new List<Class1>();
for(int i = 0 ; i < nbUsers ; i++)
{
tr = new Class1();
tr.getDetails();
sd.Add(tr);
}
Console.ReadLine();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.