Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

c# - accessing variables from another method

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

    }


}

}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 an instance variable

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

And remove it from the method

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...