分配枚举值 c#
Assign enum value c#
我有一个名为 Parrot 的 class,它继承自 Bird class,又继承自 Animal class。
Class 动物具有枚举性别类型。但是,当我尝试创建 Parrot 的新对象并像这样分配枚举值时:
Animal.cs
internal class Animal
{
private int id;
private String name;
private int age;
enum GenderType { Female, Male, Unknown }
GenderType gender;
public int Id { get => id; set => id = value; }
public string Name { get => name; set => name = value; }
public int Age { get => age; set => age = value; }
}
}
Form1.cs
.
.
pr = new parrot();
pr.Name = name.Text;
pr.Age = int.Parse(age.Text);
((parrot)pr).Color = extraInfo.Text;
((parrot)pr).FlyingSpeed = int.Parse(fSpeed.Text);
pr.gender = GenderType.Female;
.
.
Bird.cs
internal class Bird:Animal
{
private int flyingSpeed;
public int FlyingSpeed { get => flyingSpeed; set => flyingSpeed = value; }
}
}
parrot.cs
internal class parrot:Bird
{
private string color;
public string Color { get => color; set => color = value; }
}
}
我遇到了这些错误:
名称'genderType'在当前上下文中不存在
'Animal.genderType'是一种类型,但像变量一样使用
'genderType':不能通过表达式引用类型;尝试 'Animal.genderType' 而不是
类 中的内部类型,例如:
class A {
enum B { C = 0 }
}
不是变量。
如果您想要一个变量绑定到 Animal
并在需要时更改它,您需要一个变量:
class Animal {
//type
enum GenderType { ... }
//variable
GenderType gender;
}
//code:
var animal = new Animal();
animal.gender = GenderType.Female;
我有一个名为 Parrot 的 class,它继承自 Bird class,又继承自 Animal class。 Class 动物具有枚举性别类型。但是,当我尝试创建 Parrot 的新对象并像这样分配枚举值时:
Animal.cs
internal class Animal
{
private int id;
private String name;
private int age;
enum GenderType { Female, Male, Unknown }
GenderType gender;
public int Id { get => id; set => id = value; }
public string Name { get => name; set => name = value; }
public int Age { get => age; set => age = value; }
}
}
Form1.cs
.
.
pr = new parrot();
pr.Name = name.Text;
pr.Age = int.Parse(age.Text);
((parrot)pr).Color = extraInfo.Text;
((parrot)pr).FlyingSpeed = int.Parse(fSpeed.Text);
pr.gender = GenderType.Female;
.
.
Bird.cs
internal class Bird:Animal
{
private int flyingSpeed;
public int FlyingSpeed { get => flyingSpeed; set => flyingSpeed = value; }
}
}
parrot.cs
internal class parrot:Bird
{
private string color;
public string Color { get => color; set => color = value; }
}
}
我遇到了这些错误:
名称'genderType'在当前上下文中不存在
'Animal.genderType'是一种类型,但像变量一样使用
'genderType':不能通过表达式引用类型;尝试 'Animal.genderType' 而不是
类 中的内部类型,例如:
class A {
enum B { C = 0 }
}
不是变量。
如果您想要一个变量绑定到 Animal
并在需要时更改它,您需要一个变量:
class Animal {
//type
enum GenderType { ... }
//variable
GenderType gender;
}
//code:
var animal = new Animal();
animal.gender = GenderType.Female;