我需要一个自定义数据类型的订单状态,如何在C#中完成?以下是我的意思的一个例子。
订单可以是4种状态:
- 在队列中
- 处理
- 运输
- 收到
class Order {
public OrderStatus status { get; set; }
}
Order myorder = new Order();
myorder.status = OrderStatus.queue;
...
if (myorder.status == OrderStatus.received)
{
...
}
如何定义OrderStatus类,以便有四个选项可供选择?
干杯
答案 0 :(得分:4)
enum OrderStatus {
InQueue,
Handling,
Shipping,
Received
}
作为例子:
enum OrderStatus {
NewOrder = 1,
OnHold = 6,
CancelledOrder = 9,
// Approval States
WaitingApproval = 7,
ApprovalRejected = 8,
// Processing
InQueue = 2,
Handling = 3,
// We've done our job!
Shipping = 4,
Received = 5
}
答案 1 :(得分:2)
使用枚举,这是"可能值的集合"
public enum OrderStatus
{
InQueue,
Handling,
Shipping,
Received
}
该类型的变量将用作:
public OrderStatus myStatus;
myStatus = OrderStatus.Shipping;
if (myStatus == OrderStatus.Shipping)
...
答案 2 :(得分:1)
答案 3 :(得分:0)
使用枚举:
public enum Orderstatus{
InQueue,
Handling,
Shipping,
Received
}