Loop through class in C# and change property value

2019-07-25 18:02发布

问题:

I am writting a dashboard application in Asp.net. When I load the main page I capture control settings and dynamically create gauges, charts, images, etc.. and load them to a cell in an HTML table. The data that will be fed to each control will update every 3 seconds. I currently store my gauges in a class called BoldGauge. Inside of that class there is a property for GaugeValue, that will need to be updated every 3 seconds. How can I loop through the class and change the values at runtime for each gauge that was created? My class looks like this:

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

On the main page I generate the new object Like this:

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

Thanks.

回答1:

Create a collection of BoldGague objects, then loop through that collection and make the necessary changes.

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

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


回答2:

IEnumerable<Control> EnumerateControlsRecursive(Control parent)
{
    foreach (Control child in parent.Controls)
    {
        yield return child;
        foreach (Control descendant in EnumerateControlsRecursive(child))
            yield return descendant;
    }
}

and use it like this:

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


回答3:

You need a collection to store all of your gauges in. When you have that, you can simply loop through the collection and update each gauge in turn.

var gauges = new List<BoldGague>()

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

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

Or, you can put them in a dictionary, and then update them by name:

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;


回答4:

Just an FYI, with the newer auto-getters/setters, you can rewrite your exact class like this:

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