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

时间:2012-06-28 20:43:00

标签: c# asp.net webforms

我正在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);

感谢。

4 个答案:

答案 0 :(得分:1)

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

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;

答案 1 :(得分:1)

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"
        }
    }

答案 2 :(得分:1)

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

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

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

答案 3 :(得分:1)

只是一个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; }
}