简体   繁体   中英

Name does not exist in Visual Studio

So I am making a simple drink code for a class I am in and currently I am working on some try catch stuff. When a user clicks the clear order button the order is cleared. If the order is already empty then it throws an error. Unfortunately my catch if (itemTotal != 0) throws an error "the name "itemTotal" does not exist in the current context" and I have no idea what the means. Someone mind enlightening me?

        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 
    }
}

now you can use itemTotal in your click events without declaring it inside click events. if you declare variable inside event then the scope of the variable limited to that method.

You need to declare the variable itemTotal outside your checkOutButton_Click method like

    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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM