繁体   English   中英

在C#中循环遍历类并更改每个对象的属性值

[英]Loop through class in C# and change property value of each object

我在Asp.net上写一个仪表板应用程序。 当我加载主页面时,我捕获控制设置并动态创建仪表,图表,图像等..并将它们加载到HTML表格中的单元格。 将提供给每个控件的数据将每3秒更新一次。 我目前将我的仪表存放在一个名为BoldGauge的类中。 在该类的内部有一个GaugeValue属性,需要每3秒更新一次。 如何循环遍历类并在运行时为创建的每个仪表更改值? 我的班级看起来像这样:

public class BoldGauge
{
    private ASPxGaugeControl m_Gauge;
    private int m_GaugeTimer;
    private string m_GaugeValue;
    private string m_GaugeDataType;
    private float m_GaugeMinValue;
    private float m_GaugeMaxValue;

    public BoldGauge(ASPxGaugeControl Gauge, string GaugeValue, string GaugeDataType, float GaugeMinValue, float GaugeMaxValue)
    {
        m_Gauge = Gauge;
        m_GaugeValue = GaugeValue;
        m_GaugeDataType = GaugeDataType;
        m_GaugeMinValue = GaugeMinValue;
        m_GaugeMaxValue = GaugeMaxValue;

    }

    public string GaugeValue
    {
        get
        {
            return m_GaugeValue;
        }
        set
        {
            m_GaugeValue = value;
        }
    }

    public int GaugeTimer
    {
        get
        {
            return m_GaugeTimer;
        }
        set
        {
            m_GaugeTimer = value;
        }
    }

    public string GaugeDataType
    {
        get
        {
            return m_GaugeDataType;
        }
        set
        {
            m_GaugeDataType = value;
        }
    }

    public ASPxGaugeControl Gauge
    {
        get
        {
            return m_Gauge;
        }
        set
        {
            m_Gauge = value;
        }
    }

    public float GaugeMinValue
    {
        get
        {
            return m_GaugeMinValue;
        }
        set
        {
            m_GaugeMinValue = value;
        }
    }

    public float GaugeMaxValue
    {
        get
        {
            return m_GaugeMaxValue;
        }
        set
        {
            m_GaugeMaxValue = value;
        }
    }
}

在主页面上我生成新对象像这样:

newBoldGauge = new BoldGauge(boldGaugeControl, gaugeValue, controlData, minimumGaugeValue, MaximumGaugeValue);

谢谢。

您需要一个集合来存储您的所有仪表。当您拥有它时,您可以简单地遍历集合并依次更新每个仪表。

var gauges = new List<BoldGague>()

gauges.Add(new BoldGauge(boldGaugeControl, gaugeValue, controlData, 
    minimumGaugeValue, MaximumGaugeValue));
...

foreach(var gauge in gauges)
{
    // update the value
}

或者,您可以将它们放在字典中,然后按名称更新它们:

var gauges = new Dictionary<string, BoldGauge>();

gauges.add("name of gauge", new BoldGauge(boldGaugeControl, gaugeValue, 
    controlData, minimumGaugeValue, MaximumGaugeValue));

...

//  Update the value of a named gauge
gauges["name of gauge"].GaugeValue = newValue;
IEnumerable<Control> EnumerateControlsRecursive(Control parent)
{
    foreach (Control child in parent.Controls)
    {
        yield return child;
        foreach (Control descendant in EnumerateControlsRecursive(child))
            yield return descendant;
    }
}

并像这样使用它:

    foreach (Control c in EnumerateControlsRecursive(Page))
    {
        if(c is BoldGauge)
        {
            BoldGauge.GaugeValue = "some value"
        }
    }

创建一个BoldGague对象的集合,然后遍历该集合并进行必要的更改。

List<BoldGague> gagues = new List<BoldGague>();

foreach(BoldGague gague in gagues)
{
    // change values here.
}

只是一个FYI,使用较新的自动getter / setter,你可以像这样重写你的确切类:

public class BoldGauge
{
    public BoldGauge(ASPxGaugeControl gauge, string gaugeValue, string gaugeDataType, float gaugeMinValue, float gaugeMaxValue)
    {
        Gauge = gauge;
        GaugeValue = gaugeValue;
        GaugeDataType = gaugeDataType;
        GaugeMinValue = gaugeMinValue;
        GaugeMaxValue = gaugeMaxValue;    
    }

    public string GaugeValue { get; set; }
    public int GaugeTimer { get; set; }
    public string GaugeDataType { get; set; }
    public ASPxGaugeControl Gauge { get; set; }
    public float GaugeMinValue { get; set; }
    public float GaugeMaxValue { get; set; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM