【问题标题】:Class members of another class另一个班级的班级成员
【发布时间】:2011-11-16 07:17:23
【问题描述】:

我有以下类结构

 public class Limit
 {
    public double MinSelectedValue;
    public double MaxSelectedValue;
    public double MinValue;
    public double MaxValue;

 }

public class Limits
{
    public Limit Standard;
    public Limit Critical;
    public string Name;
    public string Unit;

}

我想要一个通用的类限制列表。我的问题是如何在限制类中为“限制”类型的变量赋值。

谢谢 维韦克

【问题讨论】:

  • 我会把这些字段变成属性。甚至可能是只读属性。

标签: c# class .net-3.5


【解决方案1】:

我认为您使用的是 Java 语言,所以这个答案与 Java 语言有关。

假设你这样定义列表

List<Limits> mylimits = new ArrayList<Limits>();

并且您想将 Limit 对象的值分配给 Limits 类。首先定义Limit类对象

Limit standard = new Limit();
Limit critical = new Limit();

现在像这样定义 Limits 类对象

Limits limits1 = new Limits();
limits.Standard = standard; // or you can directly define here like limits.Standard = new Limit()
limits.Critical = critical;

现在像这样将此限制对象添加到列表中

mylimits.add(limits1);

【讨论】:

    【解决方案2】:

    希望您在 C# 语言中给出的代码,在 C# 中,您可以将值分配给类型变量,如下所示。

            Limits Lmt = new Limits();
            Lmt.Standard = new Limit
            {
                 MinSelectedValue = 23.5,
                 MaxSelectedValue = 20.20,
                  MinValue = 1.23,
                   MaxValue = 23.2
            };
            Lmt.Critical = new Limit
            {
                 MinSelectedValue = 23.5,
                 MaxSelectedValue = 20.20,
                  MinValue = 1.23,
                   MaxValue = 23.2
            };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      • 2013-06-02
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多