【问题标题】:Name does not exist in Visual StudioVisual Studio 中不存在名称
【发布时间】:2015-10-17 06:00:13
【问题描述】:

所以我正在为我所在的一个班级制作一个简单的饮料代码,目前我正在研究一些尝试捕捉的东西。当用户单击清除订单按钮时​​,订单被清除。如果订单已经为空,则会引发错误。不幸的是,如果(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);
        }

    }

【问题讨论】:

  • 您可能会发现this post 在这种情况下很有帮助。

标签: c# winforms visual-studio


【解决方案1】:
public partial class Form1 : Form
{
    //move your variable to here..
    private double itemTotal;

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

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

【讨论】:

  • 哎呀我一开始有这个但没有我的私人替身,删除它,放弃并来到这里。啊,我犯了愚蠢的错误,感谢您的帮助。
【解决方案2】:

您需要在 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
}

【讨论】:

  • 您可能会发现this post 在这种情况下很有帮助。
猜你喜欢
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 2012-08-17
  • 2015-06-01
  • 2016-05-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
相关资源
最近更新 更多