Why can not reference a type through an expression?

好久不见. 提交于 2019-12-23 12:35:54

问题


The following code seems to be impossible to compile no matter how hard I am trying to cast it :P can somone please tell me what I am doing wrong?

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);
    }
}

回答1:


You're confusing a field that has an enum type with the enum type itself. Your code is about as useful as saying string="bla".

public enum OVType { OVLH, OVLL }
public class LUOverVoltage
{
    public string Name { get; set; }
    public OVType OVType { get; set; }

This declares a type called OVType and a property with the same name. Now your code should work.


As a side note, both your type names, and the property names violate .net naming guidelines.

I'd name the enum type OverVoltKind and the property just Kind.




回答2:


You're not setting a property, you're trying to set the enum.

Add a public OVType ovType and use 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);
    }
}



回答3:


You have defined an Enum inside your class. What you have not done is declare a variable to hold an instance of that 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);
    }
}



回答4:


OVType - is not a field, its a type

Try this

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);
    }
}



回答5:


OVType is the type of the variable. You have set it to enum, which is the keyword used to declare a new enum type. You need to declare OVType as an enum type, then use that as the property type.

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);
    }
}


来源:https://stackoverflow.com/questions/8048159/why-can-not-reference-a-type-through-an-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!