在属性中嵌套属性/在属性中嵌套对象

时间:2017-09-13 07:34:21

标签: c# object properties

这是一个简单的问题,但我无法在任何地方找到答案:

假设我有一个班级:

public class ControlLoop
{
    public double Setpoint { get; set; }
    public double SetpointHighvalue { get; set; }
    public double SetpointLowvalue { get; set; }
}

显然,最后两个属性与第一个属性相关。我想重新编写类,以便我可以启动嵌套的属性:

ControlLoop loop7a = new ControlLoop()
{
    Setpoint = 7.1,
    Setpoint.Highvalue = 20,
    Setpoint.Lowvalue = -10,
};

并使用它:

            loop7a.Setpoint = 7.1;
        loop7a.Setpoint.Highvalue = 20;
        loop7a.Setpoint.Lowvalue = -10;

这有可能吗?

更进一步,假设我有两个班级:

public class ControlLoop
{
    public double Setpoint { get; set; }
    public ValueRanges SetpointLimits { get; set; } = new ValueRanges();
}

public class ValueRanges
{
    public double Highvalue { get; set; }
    public double Lowvalue { get; set; }
}

我想用它来重新编程它:

ControlLoop loop7a = new ControlLoop()
{
    Setpoint = 7.1,
    Setpoint.SetpointLimits.Highvalue = 20,
    Setpoint.SetpointLimits.Lowvalue = -10,
};

这有可能吗?如何?

更新:我在这里制定了@tetralobita的建议。它给出了期望的结果,但感觉有点“过度编程”:

    class Program
{
    static void Main(string[] args)
    {

        ControlLoop loop7a = new ControlLoop()
        {
            Setpoint = new DoubleX(3.4)
            {
                PointLimits = new ValueRanges()
                {
                    Highvalue = 30,
                    Lowvalue = -40,
                },
            },
            State = new IntX(1)
            {
                PointLimits = new ValueRanges()
                {
                    Highvalue = 0,
                    Lowvalue = 8,
                },
            },

        };
        loop7a.Setpoint = 7.1;
        loop7a.Setpoint.Value = 7.1;
        loop7a.Setpoint.PointLimits.Highvalue = 20;
        loop7a.Setpoint.PointLimits.Lowvalue = -10;
        loop7a.State = 2;
        loop7a.State.PointLimits.Lowvalue = 0;
        loop7a.State.PointLimits.Highvalue = 10;


    }
}
public class ControlLoop
{
    public DoubleX Setpoint { get; set; }
    public IntX State { get; set; }
}

public class ValueRanges
{
    public Double Highvalue { get; set; }
    public Double Lowvalue { get; set; }

}


public class VariableX
{

    public ValueRanges PointLimits { get; set; } = new ValueRanges()
    {
        Highvalue = 1,
        Lowvalue = -1,
    };



}
public class DoubleX : VariableX
{
    public DoubleX(double d)
    {
        Value = d;
    }

    public Double Value { get; set; }

    public static implicit operator DoubleX(double v)
    {
        return new DoubleX(v);
    }


}
public class IntX : VariableX
{
    public IntX(int d)
    {
        Value = d;
    }

    public int Value { get; set; }

    public static implicit operator IntX(int v)
    {
        return new IntX(v);
    }


}

3 个答案:

答案 0 :(得分:3)

您在C#中可以使用ControlLoop(loop7a)的语法,但感觉不对。

请改为尝试:

public class SetPoint {
    public double Value {get;set;}
    public double High {get;set;}
    public double Low {get;set;}
}

public class ControlLoop {
    public SetPoint SetPoint {get;set;} 
}

并创建一个实例:

ControlPoint loop7a = new ControlPoint() {
    SetPoint = new SetPoint() {
        Value = 1.0,
        High = 9.0,
        Low = 0.32
    }
}

答案 1 :(得分:1)

ControlLoop loop7a = new ControlLoop()
{
    Setpoint = 7.1,
    Setpoint.Highvalue = 20,
    Setpoint.Lowvalue = -10,
};

Setpointdouble。双打没有HighvalueLowvalue属性。这使得无法访问Setpoint.HighvalueSetpoint.Lowvalue

Setpoint.SetpointLimits.Highvalue也是如此,因为双打没有SetpointLimits属性。

你能做的是这样的事情:

ControlLoop loop7a = new ControlLoop()
{
    Setpoint = new Setpoint(7.1)
    {
        Highvalue = 20,
        Lowvalue = -10,
    },
};

如果这对你来说是可以接受的,那就是它的样子:

public struct Setpoint
{
     public double Value { get; set; }
     public double Highvalue { get; set; }
     public double Lowvalue { get; set; }

     public Setpoint(double value)
     {
         Value = value;
         Highvalue = 0;
         Lowvalue = 0;
     }
}

public class ControlLoop
{
    public Setpoint Setpoint { get; set; }
}

答案 2 :(得分:1)

是的,您可以通过覆盖implicit运营商

来实现

这适用于double个数字

class Digit {
    public Digit(double d) {
        Val = d;
    }

    public double Val;

    public static implicit operator double(Digit d) {
        return d.Val;
    }

    public static implicit operator Digit(double d) {
        return new Digit(d);
    }

}

现在你可以这样做Digit d = 12; 执行此操作时,班级会创建新的Digit并将您的值分配给Val

Msdn链接是here