Visual Studio 中不存在名称 [英] Name does not exist in Visual Studio

查看:80
本文介绍了Visual Studio 中不存在名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在为我所在的班级制作一个简单的饮料代码,目前我正在研究一些尝试捕捉的东西.当用户点击清除订单按钮时​​,订单被清除.如果订单已经为空,则会引发错误.不幸的是,如果 (itemTotal != 0) 抛出错误当前上下文中不存在名称itemTotal",我不知道这是什么意思.有人介意启发我吗?

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

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

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.

这篇关于Visual Studio 中不存在名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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