Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can not reference a type through an expression?

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);
    }
}
like image 746
Saeid Yazdani Avatar asked Nov 08 '11 09:11

Saeid Yazdani


4 Answers

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.

like image 62
CodesInChaos Avatar answered Oct 19 '22 22:10

CodesInChaos


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);
    }
}
like image 35
CodeCaster Avatar answered Oct 19 '22 23:10

CodeCaster


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);
    }
}
like image 45
samjudson Avatar answered Oct 19 '22 23:10

samjudson


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);
    }
}
like image 25
Stecya Avatar answered Oct 19 '22 22:10

Stecya