1

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;
    }
}

}

2
  • 2
    Fields and properties don't work well with the ASP.NET page paradigm. When a page is loaded, that instance of a class is discarded. When the the page postsback, you get an entirely new page instance. Commented Mar 18, 2016 at 17:58
  • When you click button in asp.net it posback page so all global variable doesn't retain its value so instead use hidden field,viewstate or session Commented Mar 18, 2016 at 18:01

3 Answers 3

2

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;

Sign up to request clarification or add additional context in comments.

3 Comments

OP, Viewstate solution is better then what I suggested (session). Reading/writing (object conversion) is exactly the same.
Thank you, I'm looking into this now
After some studying, I was able to implement ViewState in my application to solve my problem. Thank you so much.
0

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);
}

1 Comment

This does clear out the textbox, but the arithmetic does not work. Do you see a problem with the way I've stored and/or accessed the textbox values?
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.