簡體   English   中英

名稱在Visual Studio中不存在

[英]Name does not exist in Visual Studio

因此,我正在為自己上的一堂課編寫一個簡單的飲品代碼,目前我正在研究一些嘗試捕捉的東西。 當用戶單擊清除訂單按鈕時​​,訂單將被清除。 如果訂單已經為空,則拋出錯誤。 不幸的是,如果(itemTotal != 0)拋出錯誤“名稱“ itemTotal”在當前上下文中不存在”,我不知道這是什么意思。 有人啟發我嗎?

        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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM