如何设置对象的名称,如果我只有枚举的数量

本文关键字:如果 枚举 何设置 设置 对象 | 更新日期: 2023-09-27 18:18:55

如何设置对象的名称,如果我只有枚举的数量!

在代码中看到我的意思我必须解释

//工作//不工作

代码
public enum CarColor
{
    Red = 0,
    Blue= 1,     
}
public class CarColor
{
    public virtual CarColor Id { get; set; }
}
public class Car
{       
    public virtual int Customnumber{ get; set; }
    public virtual CarColor CarColorNumber{ get; set; }       
}

Public SaveIt(Car car)
{
  car.CarColorNumber= CarColor.Blue;  //Working
  car.CarColorNumber= 1;  // not Workingm the color for blue
}

如何设置对象的名称,如果我只有枚举的数量

有问题的那一行已经可以用了:

// This compiles fine
car.CarColorNumber = 0;
但是,它不能编译除以外的任何整数值,而只能编译为0的常量。从常量值0到任何枚举类型都有隐式转换,但对于其他任何类型,都是显式转换。例如:
int number = 0;
// number is a variable, not a constant expression, so you need to cast.
car.CarColorNumber = (CarColor) number;