简体   繁体   中英

Boolean Not Working ASP.Net

I am trying to make a simple calculator. The boolean value in the bttn_Click() isn't working properly. The calculator is supposed to take in values and then reset when an operation is pressed. Instead, the calculator continues to append numbers to the value in the text box. As a result, the bttnEquals_Click() does not work either. I tried putting different values in the text box and determined that the the boolean is the culprit. Is there something wrong with the logic? TIA

C# file

public partial class WebForm1 : System.Web.UI.Page
{
    Boolean op_pressed = false;
    String operation = "";
    Double result = 0.0;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void bttn_Click(object sender, EventArgs e)
    {
        if (op_pressed)
        {
            txtDisplay.Text = String.Empty;
        }
        Button b = (Button)sender;
        txtDisplay.Text += b.Text;
    }

    protected void bttnClear_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = string.Empty;

    }

    protected void bttnOperation_Click(object sender, EventArgs e)
    {
        op_pressed = true;
        Button b = (Button)sender;
        operation = b.Text;
        result = Double.Parse(txtDisplay.Text);
    }

    protected void bttnEquals_Click(object sender, EventArgs e)
    {
        switch(operation)
        {
            case "+":
                txtDisplay.Text = (result + Double.Parse(txtDisplay.Text)).ToString();
                break;
            case "-":
                txtDisplay.Text = (result - Double.Parse(txtDisplay.Text)).ToString();
                break;
            case "*":
                txtDisplay.Text = (result * Double.Parse(txtDisplay.Text)).ToString();
                break;
            case "/":
                txtDisplay.Text = (result / Double.Parse(txtDisplay.Text)).ToString();
                break;
            default:
                break;
        }
        op_pressed = false;
    }
}

}

It does work you just not using it properly in ASP.NET every time you post-back it reloads the page therefore it resets your variable to original value in the situations like that you have to use View State

Understanding ASP.NET View State

something like ViewState["op_pressed"] = true;

You are not saving the value of op_preessed in viewstate or anywhere else, so every time you click a button, it will be false. that is why bttn_Click not clearing the text. The easy way to fix your issue is to clear the textbox immediately within bttnOperation_Click;

protected void bttnOperation_Click(object sender, EventArgs e)
{
    op_pressed = true;

    txtDisplay.Text = String.Empty;

    Button b = (Button)sender;
    operation = b.Text;
    result = Double.Parse(txtDisplay.Text);
}

You cannot store state in variables within the Page class and expect them to be available intact between HTTP requests. Based on what you're trying to do, I can only assume that you're learning, and that's fine (otherwise, your solution for a calculator, architecturally, is not good; but, let's continue, regardless; I'm not criticizing).

This is because, between those click-events, you have server delivering HTML to the browser, while it's not maintaining (remembering) state (result, operation, etc). In order to make it "remember" the state (again, using your architecture), you could use "ASP.NET Session".

So, for example, to store something in the session:

this.Session["op_pressed"] = true;

Then, to read back the previously stored value:

object value = this.Session["op_pressed"];
// you cannot assume it exists; it may be NULL
// so, cast (convert) to type that you believe it to be
// e.g:
bool pressed;
bool worked = bool.TryParse(this.Session["op_pressed"], out pressed);
if(!worked)
{
   pressed = false; // default; first-time
}

// process "pressed" here

In other words, move these variables into the "session":

Boolean op_pressed = false;
String operation = "";
Double result = 0.0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM