简体   繁体   English

名称在Visual Studio中不存在

[英]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. 不幸的是,如果(itemTotal != 0)抛出错误“名称“ itemTotal”在当前上下文中不存在”,我不知道这是什么意思。 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. 现在,您可以在点击事件中使用itemTotal,而无需在点击事件中声明它。 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 您需要在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.

相关问题 当前上下文中不存在名称“ViewBag” - Visual Studio 2015 - The name 'ViewBag' does not exist in the current context - Visual Studio 2015 在Visual Studio中打开旧项目时,类型名称不存在 - The type name does not exist while opening an old project in Visual studio 当前上下文中不存在名称“DependencyProperty”(Visual Studio) - The name 'DependencyProperty' does not exist in the current context (Visual Studio) Visual Studio 2012中不存在用于远程调试的计算机名称 - Machine Name for remote debugging does not exist in Visual Studio 2012 Visual Studio 2013的当前上下文中不存在名称“我的” - The Name 'My' does not exist in current context in Visual Studio 2013 当前上下文中不存在名称“CommandManager”(Visual Studio 2015) - The name “CommandManager” does not exist in the current context (Visual Studio 2015) Visual Studio:类型或命名空间名称“属性”不存在 - Visual Studio: Type or namespace name 'Properties' does not exist 我在Visual Studio 2010上收到错误消息:类型或名称空间名称'HttpUtility'不存在 - Im getting error on visual studio 2010: The type or namespace name 'HttpUtility' does not exist 升级到Visual Studio 2010之后-类型或名称空间名称'xxxxxxxxx_ascx'在名称空间'ASP'中不存在 - After upgrading to Visual Studio 2010 - The type or namespace name 'xxxxxxxxx_ascx' does not exist in the namespace 'ASP' Visual Studio Online-类型或名称空间名称yyy在名称空间xxx中不存在 - Visual Studio Online - The type or namespace name yyy does not exist in the namespace xxx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM