简体   繁体   中英

How to initialize list in form application

I'm just begginer in programming and have one question. There is a code of my Windows form app:

namespace Temperature_controller.Temperature_controller
{
    public class CPU_Temp
    {
        public double CurrentValue { get; set; }

        public static List<CPU_Temp> Temperatures
        {
            get
            {
                List<CPU_Temp> result = new List<CPU_Temp>();
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\WMI", "SELECT * FROM MSAcpi_ThermalZoneTemperature");
                foreach (ManagementObject obj in searcher.Get())
                {
                    Double temp = Convert.ToDouble(obj["CurrentTemperature"].ToString());
                    temp = (temp - 2732) / 10.0;
                    result.Add(new CPU_Temp { CurrentValue = temp });

                }
                return result;

            }
        }

    }
}

there is a form code:

namespace Temperature_controller
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CPU_Temp cPU_Temp = new CPU_Temp();

            cartesianChart1.Series = new SeriesCollection
            {
                new LineSeries
                {
                    Values=new ChartValues<ObservablePoint>
                    {
                        new ObservablePoint(0,cPU_Temp.CurrentValue),
                        new ObservablePoint(0,cPU_Temp.CurrentValue),
                        new ObservablePoint(0,cPU_Temp.CurrentValue),
                        new ObservablePoint(0,cPU_Temp.CurrentValue)
                    }
                }
            };
        }
    }
}

My question is how can I initialize the list? I don't know how to call it. I do not owe this code just found it in internet and have tried to fit to my project but it was writning i think to console application and i have no idea how to change it to work in Windows Form. Thanks for any help.

When you did this: List result = new List();

You initialized your list. I would just put it outside the curly brackets ; this way you'll always have an empty list initialized.

Another option is List.Clear() function.

Read more here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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