从其他方法访问变量

本文关键字:变量 访问 方法 其他 | 更新日期: 2023-09-27 18:31:54

我目前正在尝试从另一个加载表单方法访问双精度。一旦我更改了一个复选框,我希望从这个变量中添加/减去。我已经对变量和问题发表了评论。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Virtual_Car_Dealer
{
public partial class BMW : Form
{
    private CarDatabase database;
    public BMW()
    {
        InitializeComponent();
    }
    private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
    }
    private void picLogo_Click(object sender, EventArgs e)
    {
        var Form1 = new Form1();
        this.Hide();
        Form1.Show();
    }
    private void picLogo_MouseEnter(object sender, EventArgs e)
    {
        picLogo.BorderStyle = BorderStyle.FixedSingle;
        this.Cursor = Cursors.Hand;
    }
    private void picLogo_MouseLeave(object sender, EventArgs e)
    {
        picLogo.BorderStyle = BorderStyle.Fixed3D;
        this.Cursor = Cursors.Default;
    }
    private void BMW_FormClosed(object sender, FormClosedEventArgs e)
    {
        //Needs Work
    }
    private void BMW_Load(object sender, EventArgs e)
    {
        database = new CarDatabase();
        database.Show();
        database.Hide();
        rdbStandard.Checked = true;
        int carID = 0;
        string value = database.dgvBMW.Rows[carID].Cells["ID"].Value.ToString();
        string Model = database.dgvBMW.Rows[carID].Cells["Model"].Value.ToString();
        string Stock = database.dgvBMW.Rows[carID].Cells["Stock"].Value.ToString();
        string Price = database.dgvBMW.Rows[carID].Cells["Price"].Value.ToString();
        string PicLocation = database.dgvBMW.Rows[carID].Cells["Picture Location"].Value.ToString();
        txtCarName.Text = Model;
        picCar.ImageLocation = PicLocation;
        int CarStock;
        int.TryParse(Stock, out CarStock);
        if (CarStock <= 3)
        {
            lblStock.ForeColor = Color.Red;
            lblStock.Text = "Hurry there's only " + CarStock + " cars availiable!";
        }
        else
        {
            lblStock.ForeColor = Color.Green;
            lblStock.Text = "There are " + CarStock + " cars availiable!";
        }

        double carPrice;//the variable
        double.TryParse(Price, out carPrice);
        lblPrice.Text = "Cost of car - £" + carPrice;
        lblTotalPrice.Text = "£" + carPrice;
    }

    private void btnAccept_Click(object sender, EventArgs e)
    {
        for (int rows = 0; rows < database.dgvBMW.Rows.Count; rows++)
        {
            for (int col = 0; col < database.dgvBMW.Rows[rows].Cells.Count; col++)
            {
                string value = database.dgvBMW.Rows[rows].Cells["model"].Value.ToString();
            }
        }
    }
    public void chkAuto_CheckedChanged(object sender, EventArgs e)
    {

        if (chkAuto.Checked = true)
        {
            carPrice = carPrice + 1300;//the problem
    }

}

}

底部

的方法错误状态。 "carPrice"这个名字在当前上下文中不存在。提前致谢

从其他方法访问变量

DaveDev 答案是正确的 移出变量

public void chkAuto_CheckedChanged(object sender, EventArgs e)
{

    if (chkAuto.Checked = true)
    {
        carPrice = carPrice + 1300;//because is declared inside another method
}

使carPrice成为实例变量

public partial class BMW : Form
{
   private CarDatabase database;
   private double carPrice;
   ...

并将其从方法中删除

private void BMW_Load(object sender, EventArgs e)
{
    ...
    // double carPrice;
    ...

您需要将carPrice移出其当前范围才能成为该类的私有字段。