使用C#-在switch case语句后更改值的交付程序
首先我要解释一下我的程序 我已经编写了一个邮件传递程序,它允许用户开始对包进行传递,并保存诸如发件人名称和发件人目的地之类的值,然后列出包,然后开始对包进行传递 这里有一个名为senderDestination的公共类,我在其中从列表中将值保存到使用C#-在switch case语句后更改值的交付程序,c#,C#,首先我要解释一下我的程序 我已经编写了一个邮件传递程序,它允许用户开始对包进行传递,并保存诸如发件人名称和发件人目的地之类的值,然后列出包,然后开始对包进行传递 这里有一个名为senderDestination的公共类,我在其中从列表中将值保存到 public class senderDestination { public string senderName { get; set; } public string destination { get; s
public class senderDestination
{
public string senderName { get; set; }
public string destination { get; set; }
public string delivered;
public string pending;
public string delivering;
public senderDestination(string status, string pendingStatus, string startingDelivery)
{
delivered = status;
pending = pendingStatus;
delivering = startingDelivery;
}
}
这里我有一个switch语句,在这个switch语句中,我允许用户输入值,例如要传递的目的地名称和我们要发送给的人的名称:
case ConsoleKey.D2:
Clear();
bool registeringSenderAndDestination = true;
do
{
senderDestination senderdestination = new senderDestination("Delivered", "Pending Delivery", "Starting Delivery");
Write("Sender: ");
senderdestination.senderName = ReadLine();
Write("Destination: ");
senderdestination.destination = ReadLine();
Clear();
WriteLine($"Sender: {senderdestination.senderName}");
WriteLine($"Destination: {senderdestination.destination}");
allSenders.Add(senderdestination);
} while (registeringSenderAndDestination);
Clear();
break;
在我的第三个switch case语句中,我允许用户列出我们刚刚保存的所有包:
case ConsoleKey.D3:
Clear();
int id = 1;
WriteLine("ID Destination Status");
WriteLine("------------------------------------------------------------------");
// List packages
foreach (var sender in allSenders)
{
// write here all the information you want to display.
WriteLine($"{id++} {sender.destination,-14} {sender.pending}");
}
ReadKey(true);
Clear();
break;
第四个switch语句是用户想要开始交付这些包时单击的位置:
case ConsoleKey.D4:
Clear();
int ids = 1;
WriteLine("Starting delivery on pending packages...");
WriteLine("------------------------------------------------------------------");
WriteLine("ID Destination Status");
WriteLine("------------------------------------------------------------------");
foreach (var sender in allSenders)
{
WriteLine($"{ids++} {sender.destination,-14} {sender.delivering}");
}
Thread.Sleep(4000);
Clear();
break;
我要做的是,当用户单击菜单中的第四个switch语句时,我希望第三个switch语句从{sender.pending}更改为{sender.delivered},但我不知道如何执行此操作。我能做到吗?很抱歉,如果我在这个问题上有很多代码,我不知道如何解释它。首先,请注意,要交付的每个项目的状态只有一个,因此您只需要一个属性来定义此状态,而不是三个。在此上下文中,添加具有三种可能状态的枚举,并更改senderDestination类以删除这三个字符串,并添加Status属性以定义列表中项目的当前状态
public enum SenderStatus
{
delivered = 1,
pending = 2,
delivering = 3
}
public class senderDestination
{
public string senderName { get; set; }
public string destination { get; set; }
public SenderStatus Status { get;set;}
public senderDestination()
{
Status = SenderStatus.pending;
}
}
现在您可以列出列表中每个元素的状态,只需打印出status属性(WriteLine会自动将枚举转换为正确的字符串)
最后,在交付所有项目时,可以将Status属性更改为从枚举中获取的适当值
foreach (var sender in allSenders)
{
sender.Status = SenderStatus.delivered
WriteLine($"{ids++} {sender.destination,-14} {sender.Status}");
}
谢谢你,我的朋友!它正是我所需要的:)
foreach (var sender in allSenders)
{
sender.Status = SenderStatus.delivered
WriteLine($"{ids++} {sender.destination,-14} {sender.Status}");
}