Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么不能通过表达式引用类型?_C#_Types_Casting_Enums - Fatal编程技术网

C# 为什么不能通过表达式引用类型?

C# 为什么不能通过表达式引用类型?,c#,types,casting,enums,C#,Types,Casting,Enums,下面的代码似乎是不可能编译的,无论我多么努力地试图铸造它:p有人能告诉我我做错了什么吗 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,

下面的代码似乎是不可能编译的,无论我多么努力地试图铸造它:p有人能告诉我我做错了什么吗

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);
    }
}
公共级电压
{
公共字符串名称{get;set;}
公共枚举OVType{OVLH,OVLL}
public List PinGroups=new List();
公共void添加(字符串名称、OVType类型、字符串Grp)
{
this.Name=Name;
this.OVType=type;//为什么不能通过表达式引用类型?
添加(Grp);
}
}

OVType
是变量的类型。您已将其设置为,这是用于声明新枚举类型的关键字。您需要将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);
    }
}
public enum OVType{OVLH,OVLL}
公共级电压
{
公共字符串名称{get;set;}
公共OVType OVType{get;set;}
public List PinGroups=new List();
公共void添加(字符串名称、OVType类型、字符串Grp)
{
this.Name=Name;
this.OVType=type;
添加(Grp);
}
}

您不是在设置属性,而是在尝试设置枚举

添加一个
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);
    }
}
公共级电压
{
公共枚举OVType{OVLH,OVLL}
公共字符串名称{get;set;}
公共卵型;
public List PinGroups=new List();
公共void添加(字符串名称、OVType类型、字符串Grp)
{
this.Name=Name;
this.ovType=type;
添加(Grp);
}
}

您将具有枚举类型的字段与枚举类型本身混淆。您的代码就像说
string=“bla”
一样有用

这声明了一个名为
OVType
的类型和一个同名的属性。现在您的代码应该可以工作了


作为旁注,您的类型名和属性名都违反了.net命名准则


我将枚举类型命名为OverVoltKind,属性只是
Kind

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);
    }
}
公共级电压
{
公共字符串名称{get;set;}
公共OVType类型{get;set;}
公共枚举OVType{OVLH,OVLL}
public List PinGroups=new List();
公共void添加(字符串名称、OVType类型、字符串Grp)
{
this.Name=Name;
this.Type=Type;
添加(Grp);
}
}

您已经在类中定义了一个
枚举。您没有做的是声明一个变量来保存该枚举的实例

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);
    }
}
public enum OVType{OVLH,OVLL}
公共级电压
{
公共字符串名称{get;set;}
公共OVType OVType{get;set;}
public List PinGroups=new List();
公共void添加(字符串名称、OVType类型、字符串Grp)
{
this.Name=Name;
this.OVType=type;//设置属性,而不是枚举定义
添加(Grp);
}
}

您会遇到什么编译错误?'OVType':无法通过表达式引用类型;很抱歉,但这并不能使它变得更好。公共OVType OVType{OVLH,OVLL}不会编译!好像我错过了。现在更新。
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);
    }
}