从另一种方法访问变量 [英] accessing variables from another method

查看:166
本文介绍了从另一种方法访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从另一个加载表单方法访问一个double。一旦我更改一个复选框,我希望从此变量添加/减去。我已经评论过变量和问题。

I'm currently trying to access a double from another load form method. Once I change a check box I wish to add/subtract from this variable. I have commented on both the variable and the problem.

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的名称并不存在。
提前感谢

the error with the method at the bottom states. the name 'carPrice' does not exsist in the current context. thanks in advance

推荐答案

DaveDev答案正确移出变量

DaveDev answer is correct move out the variable

public void chkAuto_CheckedChanged(object sender, EventArgs e)
{


    if (chkAuto.Checked = true)
    {

        carPrice = carPrice + 1300;//because is declared inside another method

}

Make carPrice 一个实例变量

Make carPrice an instance variable

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

从方法中删除它

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

这篇关于从另一种方法访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆