以下代码似乎无法编译,无论我多么努力地编译它:P可以somone请告诉我我做错了什么?
public class LUOverVoltage
{
public string Name { get; set; }
public enum OVType { OVLH, OVLL }
public List<string> PinGroups = new List<string>();
public void Add(string name, OVType type, string Grp)
{
this.Name = name;
this.OVType = type; //Why cannot reference a type through an expression?
PinGroups.Add(Grp);
}
}
答案 0 :(得分:14)
您将具有枚举类型的字段与枚举类型本身混淆。您的代码与说string="bla"
一样有用。
public enum OVType { OVLH, OVLL }
public class LUOverVoltage
{
public string Name { get; set; }
public OVType OVType { get; set; }
这声明了一个名为OVType
的类型和一个具有相同名称的属性。现在你的代码应该可以工作了。
作为旁注,您的类型名称和属性名称都违反了.net命名准则。
我将枚举类型命名为OverVoltKind
,将属性命名为Kind
。
答案 1 :(得分:4)
您没有设置属性,而是尝试设置枚举。
添加public OVType ovType
并使用this.ovType = type
。
public class LUOverVoltage
{
public enum OVType { OVLH, OVLL }
public string Name { get; set; }
public OVType ovType;
public List<string> PinGroups = new List<string>();
public void Add(string name, OVType type, string Grp)
{
this.Name = name;
this.ovType = type;
PinGroups.Add(Grp);
}
}
答案 2 :(得分:2)
您已在班级中定义了Enum
。你还没做的是声明一个变量来保存那个枚举的实例。
public enum OVType { OVLH, OVLL }
public class LUOverVoltage
{
public string Name { get; set; }
public OVType OVType { get; set; }
public List<string> PinGroups = new List<string>();
public void Add(string name, OVType type, string Grp)
{
this.Name = name;
this.OVType = type; // setting the property, not the enum definition
PinGroups.Add(Grp);
}
}
答案 3 :(得分:1)
OVType - 不是字段,是类型
试试这个
public class LUOverVoltage
{
public string Name { get; set; }
public OVType Type {get; set;}
public enum OVType { OVLH, OVLL }
public List<string> PinGroups = new List<string>();
public void Add(string name, OVType type, string Grp)
{
this.Name = name;
this.Type = type;
PinGroups.Add(Grp);
}
}
答案 4 :(得分:0)
OVType
是变量的类型。您已将其设置为enum,这是用于声明新枚举类型的关键字。您需要将OVType声明为枚举类型,然后将其用作属性类型。
public enum OVType { OVLH, OVLL }
public class LUOverVoltage
{
public string Name { get; set; }
public OVType OVType { get; set; }
public List<string> PinGroups = new List<string>();
public void Add(string name, OVType type, string Grp)
{
this.Name = name;
this.OVType = type;
PinGroups.Add(Grp);
}
}