保持变量值

时间:2013-05-28 12:22:41

标签: c# asp.net

我正处于asp.net的学习阶段,所以决定做一个在线计算器。问题是当我进行计算时1 + 5 =它根本不提供任何输出。我尝试了调试。

Click button 1 : 
               first value = 1;
click button + : 
                first value = null;
click button 5 :
                first value = 5
click button = 
              NOTHING :)

这是我的c#代码:

public partial class _Default : System.Web.UI.Page 
{
    string firstOperand;
    string secondOperand;
    string Operator;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnOff_Click(object sender, EventArgs e)
    {
        txtScreen.Enabled = false;
        ClearVariables();
    }
    protected void btnOn_Click(object sender, EventArgs e)
    {
        txtScreen.Enabled = true;
        ClearVariables();
    }
    private void ClearVariables()
    {
        firstOperand = "";
        secondOperand = "";
        Operator = "";
    }
    protected void Operand(string value)
    {
        if (value == null) return;
        try
        {
            txtScreen.Text = value;
            if (firstOperand == null)
            {
                firstOperand = value;
            }
            else
            {
                if (Operator == null)
                {
                    firstOperand.Insert(firstOperand.Length, value);
                }
                else
                {
                    secondOperand.Insert(secondOperand.Length, value);
                }
            }
        }
        catch (Exception ex)
        {
        }

    }
    protected void Num1_Click(object sender, EventArgs e)
    {
        txtScreen.Text = Num1.Text;
        Operand(Num1.Text);

    }
    protected void Num2_Click(object sender, EventArgs e)
    {
        txtScreen.Text = Num2.Text;
        Operand(Num2.Text);
    }
    protected void Num3_Click(object sender, EventArgs e)
    {
        txtScreen.Text = Num3.Text;
        Operand(Num3.Text);

    }
    protected void Num4_Click(object sender, EventArgs e)
    {
        txtScreen.Text = Num4.Text;
        Operand(Num4.Text);
    }
    protected void Num5_Click(object sender, EventArgs e)
    {
        txtScreen.Text = Num5.Text;
        Operand(Num5.Text);
    }
    protected void Num6_Click(object sender, EventArgs e)
    {
        txtScreen.Text = Num6.Text;
        Operand(Num6.Text);

    }
    protected void Num7_Click(object sender, EventArgs e)
    {
        txtScreen.Text = Num7.Text;
        Operand(Num7.Text);
    }
    protected void Num8_Click(object sender, EventArgs e)
    {
        txtScreen.Text = Num8.Text;
        Operand(Num8.Text);
    }
    protected void Num9_Click(object sender, EventArgs e)
    {
        txtScreen.Text = Num9.Text;
        Operand(Num9.Text);
    }
    protected void Num0_Click(object sender, EventArgs e)
    {
        txtScreen.Text = Num0.Text;
        Operand(Num0.Text);
    }


    protected void btnClr_Click(object sender, EventArgs e)
    {
        txtScreen.Text = "";
        ClearVariables();
    }
    protected void OpDiv_Click(object sender, EventArgs e)
    {
        if (firstOperand != null)
        {
            txtScreen.Text = "";
            Operator = OpDiv.Text;

        }
    }
    protected void OpMul_Click(object sender, EventArgs e)
    {
        if (firstOperand != null)
        {
            txtScreen.Text = "";
            Operator = OpMul.Text;

        }
    }
    protected void OpSub_Click(object sender, EventArgs e)
    {
        if (firstOperand != null)
        {
            txtScreen.Text = "";
            Operator = OpSub.Text;

        }
    }
    protected void OpAdd_Click(object sender, EventArgs e)
    {
        if (firstOperand != null)
        {
            txtScreen.Text = "";
            Operator = OpAdd.Text;

        }
    }
    protected void OpEqual_Click(object sender, EventArgs e)
    {
        if (firstOperand == null && Operator == null)
        {
            return;
        }
        else if (firstOperand != null && Operator != null && secondOperand == null)
        {
            secondOperand = firstOperand;
        }
        else
        {
            double num1;
            double num2;
            try
            {
                num1 = Double.Parse(firstOperand);
                num2 =Double.Parse(secondOperand);
                {
                    switch (Operator)
                    {
                        case "+":
                            num1 += num2;
                            firstOperand = num1.ToString();
                            txtScreen.Text = firstOperand;
                            break;
                        case "-":
                            num1 -= num2;
                            firstOperand = num1.ToString();
                            txtScreen.Text = firstOperand;
                            break;
                        case "/":
                            if (num2 == 0)
                            {
                                txtScreen.Text = "Divison by zero";

                            }
                            else
                            {
                                num1 /= num2;
                                firstOperand = num1.ToString();
                                txtScreen.Text = firstOperand;
                            }
                            break;
                        case "*":
                            num1 *= num2;
                            firstOperand = num1.ToString();
                            txtScreen.Text = firstOperand;
                            break;
                        default: txtScreen.Text = "Invalid Operation";

                            break;

                    }
                }
            }
            catch (Exception ex)
            {
                txtScreen.Text = "Not a valid Number";
                ClearVariables();
            }
        }
        ClearVariables();
    }
    protected void OpDot_Click(object sender, EventArgs e)
    {
        if (firstOperand != null)
        {
            if (Operator == null)
            {
                firstOperand.Insert(firstOperand.Length, ".");
            }
            else
            {
                secondOperand.Insert(secondOperand.Length, ".");
            }
        }
    }
}

有人可以解释发生了什么吗?以及如何解决这个问题。

由于

3 个答案:

答案 0 :(得分:2)

行。这里很简单,你的价值在回发时令人耳目一新。所以只需在viewstate中保存值即可。 在此之前,请减少代码行。

你有

protected void Num5_Click(object sender, EventArgs e)
{
    txtScreen.Text = Num5.Text;
    Operand(Num5.Text);
}

这样的10个事件。所以首先要做一个像

这样的事件
 protected void Num_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    txtScreen.Text = btn.Text;
    Operand(btn.Text);
}

并将此事件指定为每个数字按钮的Click事件

现在在Operand方法中制作类似

的内容
  private void Operand(string value)
  {

    if(ViewState["FirstOperand"] == null)
         ViewState["FirstOperand"] = value;
    else if(ViewState["SecondOperand"] == null)
         ViewState["SecondOperand"] = value;

    }

同样减少add,sub,mul,divide运算符点击事件的代码,正如我刚刚在数字按钮点击事件中所示。 并在ViewState [“Operator”]中设置运算符值。

最后在你的OpEqual_Click事件中。初始设置第一个和第二个操作数,如

if(ViewState["FirstOperand"] != null)
firstOperand = ViewState["FirstOperand"].ToString();


if(ViewState["SecondOperand"] != null)
secondOperand = ViewState["SecondOperand"].ToString();

if(ViewState["Operator"] != null)
Operator = ViewState["Operator"].ToString();

希望这有帮助

答案 1 :(得分:1)

在我看来,您的问题出在ASP.Net环境和/或IDE中,而不是代码中。我不太了解ASP.Net,但我知道C#,我注意到这两个奇怪的事实:

  • 您的调试显示firstOperand被重置为null或每次事件后的当前操作数。

  • 但是,您将代码从不设置为firstOperandnull。它确实将其设置为""中的空字符串(clearVariables),但这与null("" != null)不同。

因此,我必须得出结论,除了代码之外的其他内容正在将firstOperand设置为null。最合乎逻辑的来源是你的执行/调试环境,它会在初始化执行时或者调用新的Page,Class,Method等时将所有对象和字符串变量重置为null(对于任何作用于这些变量的变量) )。

当然,你不希望它为每次点击按钮都这样做,所以我必须假设你的环境/设置中有一些错误导致了这一点。

希望有人比我更了解ASP.Net可以解释其余的......

答案 2 :(得分:0)

是的,我终于找到了它。

这是sessions的问题。每次单击按钮,都会调用一个新会话并重置所有值。所以我们需要在会话中添加值并恢复它。

喜欢:

 Session["Calc"] = firstOperand + ",";
 Session["Calc"] += secondOperand + ",";
 Session["Calc"] += Operator + ",";

并在页面加载时:

try
    {
       var Data = Session["Calc"].ToString().Split(',');
        if(Data[0] != "")
            firstOperand = Data[0];
        if (Data[1] != "")
        Operator = Data[1];
        if (Data[2] != "")
        secondOperand = Data[2];
    }
    catch(Exception ex)
    {
    }

我认为这不是一个好的解决方案(仍在研究asp :))。我可以使用if条件,因为项目数固定为3.