变量是只读错误

时间:2015-08-02 21:31:38

标签: c# .net

buttonHandler方法中,我需要能够appliances[tagValue].active = true;,但是当我这样做时,我收到错误,说该变量是只读的。我需要更改什么才能使其不是只读的?如果有人可以给我看或给出一个例子,那将非常感激。

private void frmPowerViewer_Load(object sender, EventArgs e)
    {
        //List<string> Appliances = File.ReadAllLines(@"\\psf\Home\Desktop\School\Advanced C#\Unit2\Program1\Watts.csv").ToList()
        //List<ApplianceDA> newAppliances = new List<ApplianceDA>();
        var appliances = new List<ApplianceDA>();

        using (var file = new System.IO.StreamReader(@"\\psf\Home\Desktop\School\Advanced C#\Unit2\Program1\Watts.csv"))
        {
            while (file.Peek() != -1)
            {
                var line = file.ReadLine();
                var fields = line.Split(',');

                var obj = new ApplianceDA(fields[0], Convert.ToInt32(fields[1]), false);
                appliances.Add(obj);
            }

            file.Close();
        }

        displayButtons(appliances);

    }

    private void displayButtons(List<ApplianceDA> appliances)
    {
        Button[] appliancesButtons = new Button[14];

        int yLocation = 200;
        int yLocation2 = 250;
        int xLocation2 = 0;
        int yLocation3 = 300;
        int xLocation3 = 0;
        // now loop through the array of buttons and add a new button and where it goes on the form.
        for (int x = 0; x <= 5; x++)
        {
            appliancesButtons[x] = new Button();  // This creates a new button and placed it in the array
            appliancesButtons[x].Text = Convert.ToString(appliances[x].name); // This adds the text that goes on the button.
            appliancesButtons[x].Location = new Point((x * 90), yLocation); // This sometimes is the hard part. Where does it go? First arg is left second arg is top of current button
            appliancesButtons[x].Tag = x; // you can store all kinds of stuff in the tab property even objects. In this case it is just a number.
            appliancesButtons[x].Click += (sender, e) => { buttonHandler(sender, e, appliances); }; // The button needs a click event handler. This wires up to the method buttonHandler.
            Controls.Add(appliancesButtons[x]); // this adds the button to the controls colection of the form.
        }

        for (int x = 6; x <= 11; x++)
        {
            appliancesButtons[x] = new Button();  // This creates a new button and placed it in the array
            appliancesButtons[x].Text = Convert.ToString(appliances[x].name); // This adds the text that goes on the button.
            appliancesButtons[x].Location = new Point((xLocation2 * 90), yLocation2); // This sometimes is the hard part. Where does it go? First arg is left second arg is top of current button
            appliancesButtons[x].Tag = x; // you can store all kinds of stuff in the tab property even objects. In this case it is just a number.
            appliancesButtons[x].Click += (sender, e) => { buttonHandler(sender, e, appliances); }; // The button needs a click event handler. This wires up to the method buttonHandler.
            Controls.Add(appliancesButtons[x]); // this adds the button to the controls colection of the form.
            xLocation2++;
        }

        for (int x = 12; x <= 13; x++)
        {
            appliancesButtons[x] = new Button();  // This creates a new button and placed it in the array
            appliancesButtons[x].Text = Convert.ToString(appliances[x].name); // This adds the text that goes on the button.
            appliancesButtons[x].Location = new Point((xLocation3 * 90), yLocation3); // This sometimes is the hard part. Where does it go? First arg is left second arg is top of current button
            appliancesButtons[x].Tag = x; // you can store all kinds of stuff in the tab property even objects. In this case it is just a number.
            appliancesButtons[x].Click += (sender, e) => { buttonHandler(sender, e, appliances); }; // The button needs a click event handler. This wires up to the method buttonHandler.
            Controls.Add(appliancesButtons[x]); // this adds the button to the controls colection of the form.
            xLocation3++;
        }
    }

    private void buttonHandler(object sender, EventArgs e, List<ApplianceDA> appliances)
    {
        Button clickedButton;  // This is a local instance variable for the button that was clicked.
        int tagValue;          // This will hold what we stuffed in the tag of the button.

        clickedButton = (Button)sender;  // The sender needs to be cast into a button type
        tagValue = (int)clickedButton.Tag; // Now cast the tag value into a integer

        int price;
        if (CheckText(txtPricePerKWH.Text) == true)
        {
            price = Convert.ToInt16(txtPricePerKWH.Text);

            if (appliances[tagValue].active == true) {
                clickedButton.BackColor = DefaultBackColor;
                txtCurrentWattage.Text = Convert.ToString(Convert.ToInt16(txtCurrentWattage.Text) - Convert.ToInt16(appliances[tagValue].watt));
                clickedButton.Text = appliances[tagValue].name;
            }
            else
            {
                clickedButton.BackColor = Color.LimeGreen;
                clickedButton.Text = Convert.ToString(appliances[tagValue].watt + " Watt");
                txtCurrentWattage.Text = Convert.ToString(Convert.ToInt16(txtCurrentWattage.Text) + Convert.ToInt16(appliances[tagValue].watt));

                //Display Pricing
                txtCostPerHour.Text = Convert.ToString((Convert.ToInt16(txtCurrentWattage.Text) *.001) * Convert.ToInt16(txtPricePerKWH.Text));
                txtCostPerDay.Text = Convert.ToString(((Convert.ToInt16(txtCurrentWattage.Text) * .001) * Convert.ToInt16(txtPricePerKWH.Text))*24);
                txtCostPerYear.Text = Convert.ToString(((Convert.ToInt16(txtCurrentWattage.Text) * .001) * Convert.ToInt16(txtPricePerKWH.Text)) * 8760);
                //appliances[tagValue].active = true;
            }

        }
    }

以下是ApplianceDA类代码:

class ApplianceDA
{
    private string mName;
    private int mWatt;
    private bool mActive;
    public string name { get { return mName;} }
    public int watt { get { return mWatt;} }
    public bool active { get { return mActive; } }

    public ApplianceDA(string theName, int theWatt, bool theActive)
    {
        this.mName = theName;
        this.mWatt = theWatt;
        this.mActive = theActive;
    }
}

1 个答案:

答案 0 :(得分:0)

查看active课程中的ApplianceDA属性。显然它是只读的。

您必须为活动属性添加setter。

public bool active 
{ 
    get { return mActive; } 
    set { mActive = value;}
} 
相关问题