保持变量值

本文关键字:变量值 | 更新日期: 2023-09-27 18:15:46

我正在学习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, ".");
            }
        }
    }
}
谁能解释一下发生了什么事?和如何解决相同的。

谢谢

保持变量值

OK。这里很简单,您的值在回发时刷新。所以只需在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事件

现在在操作数方法中输入类似

的内容
  private void Operand(string value)
  {
    if(ViewState["FirstOperand"] == null)
         ViewState["FirstOperand"] = value;
    else if(ViewState["SecondOperand"] == null)
         ViewState["SecondOperand"] = value;
    }

类似地减少了添加、删除、删除、除法操作符单击事件的代码,就像我上面展示的数字按钮单击事件一样。并在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();

希望能有所帮助

在我看来,你的问题是在你的ASP。Net环境和/或您的IDE,而不是您的代码。我不懂ASP。Net很好,但是如果我知道c#并且我注意到这两个奇怪的事实:

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

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

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

当然,你不希望它做的每一个按钮点击,所以我不得不假设有一些设置错误在你的环境/设置,导致这一点。

希望,有人知道ASP。如果我做得更好,我可以解释其余的…

我终于找到了。

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

Like:

 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。