Visual Studio 中不存在名称

Name does not exist in Visual Studio

所以我正在为我所在的 class 制作一个简单的饮料代码,目前我正在研究一些 try catch 的东西。当用户点击清除订单按钮时​​,订单被清除。如果订单已经为空,则会抛出错误。不幸的是,如果 (itemTotal != 0) 抛出错误 "the name "itemTotal" does not exist in the current context" 我不知道这意味着什么。有人介意启发我吗?

        private void checkOutButton_Click(object sender, EventArgs e)
    {

        double drinkPrice = 0.0;
        double itemTotal = 0.0;
        double smDrink = 3.00;
        double mdDrink = 3.50;
        double lgDrink = 4.00;
        int intQuantity;
        string strMessage;

        if (smallRB.Checked)
        {
            drinkPrice = smDrink;
        }
        else if (mediumRB.Checked)
        {
            drinkPrice = mdDrink;
        }
        else if (largeRB.Checked)
        {
            drinkPrice = lgDrink;
        }
        else
        {
            MessageBox.Show("Please make a size selection", "Selection Required",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        double additive = 2.50;

        if (vpCB.Checked)
        {
            drinkPrice = drinkPrice + additive;

            if (ebCB.Checked)
            {
                drinkPrice = drinkPrice + additive;
                if (cdCB.Checked)
                {
                    drinkPrice = drinkPrice + additive;
                }
            }
        }



        //Calculate extended price and add to order total
        if (quantityTextBox.Text != "")       //Not blank
        {
            try
            {
                intQuantity = int.Parse(quantityTextBox.Text);
                itemTotal = drinkPrice * intQuantity;
                totalDueTextBox.Text = itemTotal.ToString("C");
            }
            catch (FormatException err)
            {
                strMessage = "Nonnumeric data entered for quantity.";
                MessageBox.Show(strMessage, "Data Entry Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                quantityTextBox.Focus();
            }
            catch
            {
                strMessage = "Calculation error.";
                MessageBox.Show(strMessage, "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        else           //Missing data
        {
            strMessage = "Enter the quantity.";
            MessageBox.Show(strMessage, "Data entry error",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            quantityTextBox.Focus();
        }//end if
    }


    private void clearOrderButton_Click(object sender, EventArgs e)
    {
        //Clear appropriate controls

       if (itemTotal != 0)           //User should not be able to clear if not yet calculated 
        {
            veggieRB.Checked = true;    //All others are false automatically
            smallRB.Checked = false;
            mediumRB.Checked = false;
            largeRB.Checked = false;
            vpCB.Checked = false;
            ebCB.Checked = false;
            cdCB.Checked = false;
            totalDueTextBox.Text = "";
            quantityTextBox.Focus();
        }
        else
        {
            MessageBox.Show("No New Order to Clear", "Customer Order", MessageBoxButtons.OK);
        }

    }
public partial class Form1 : Form
{
    //move your variable to here..
    private double itemTotal;

    public Form1()
    {
        InitializeComponent();
        itemTotal=0; //set initial value 
    }
}

现在您可以在点击事件中使用 itemTotal,而无需在点击事件中声明它。如果您在事件中声明变量,则变量的范围仅限于该方法。

您需要在 checkOutButton_Click 方法之外声明变量 itemTotal,例如

    double itemTotal = 0.0;

private void checkOutButton_Click(object sender, EventArgs e)
{

    double drinkPrice = 0.0;

    double smDrink = 3.00;
    double mdDrink = 3.50;
    double lgDrink = 4.00;
    int intQuantity;
    string strMessage;

    if (smallRB.Checked)
    {
        drinkPrice = smDrink;
    }
    else if (mediumRB.Checked)
    {
        drinkPrice = mdDrink;
    }
    else if (largeRB.Checked)
    {
        drinkPrice = lgDrink;
    }
    else
    {
        MessageBox.Show("Please make a size selection", "Selection Required",
            MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    double additive = 2.50;

    if (vpCB.Checked)
    {
        drinkPrice = drinkPrice + additive;

        if (ebCB.Checked)
        {
            drinkPrice = drinkPrice + additive;
            if (cdCB.Checked)
            {
                drinkPrice = drinkPrice + additive;
            }
        }
    }



    //Calculate extended price and add to order total
    if (quantityTextBox.Text != "")       //Not blank
    {
        try
        {
            intQuantity = int.Parse(quantityTextBox.Text);
            itemTotal = drinkPrice * intQuantity;
            totalDueTextBox.Text = itemTotal.ToString("C");
        }
        catch (FormatException err)
        {
            strMessage = "Nonnumeric data entered for quantity.";
            MessageBox.Show(strMessage, "Data Entry Error",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            quantityTextBox.Focus();
        }
        catch
        {
            strMessage = "Calculation error.";
            MessageBox.Show(strMessage, "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    else           //Missing data
    {
        strMessage = "Enter the quantity.";
        MessageBox.Show(strMessage, "Data entry error",
            MessageBoxButtons.OK, MessageBoxIcon.Information);
        quantityTextBox.Focus();
    }//end if
}