Calculator C# ASP.NET

Hemanth B 886 Reputation points
2021-06-21T12:28:10.94+00:00

Hi, I created a calculator in C# ASP.NET Web Forms. This is my code:

I created click events for multiple buttons.

Code when numbers 0 to 9 including decimal point are clicked:
protected void numbers(object sender, EventArgs e)
{
Button b = (Button)sender;

            if (TextBox2.Text == "0")
                TextBox2.Text = "";
            if (b.Text == ".")
            {
                if (!TextBox2.Text.Contains("."))
                    TextBox2.Text = TextBox2.Text + b.Text;

            }
            else
            {
                TextBox2.Text = TextBox2.Text + b.Text;
            }
        }

Code when operators +, - , x, /, ^ are clicked:
protected void operators(object sender, EventArgs e)
{

            Button b = (Button)sender;
            firstnum = Convert.ToDouble(TextBox2.Text);
            secondnum = Convert.ToDouble(TextBox2.Text);
            operation = b.Text;
            TextBox2.Text = "";
        }

Code when the = button is clicked:

protected void Equalto_Click(object sender, EventArgs e)
            {
                firstnum = Convert.ToDouble(TextBox2.Text);
                secondnum = Convert.ToDouble(TextBox2.Text);
                switch (operation)
                {
                    case "+":
                        TextBox2.Text = Convert.ToString(firstnum + secondnum);
                        break;


                    case "-":
                        TextBox2.Text = Convert.ToString(firstnum - secondnum);
                        break;

                    case "x":

                        TextBox2.Text = Convert.ToString(firstnum * secondnum);
                        break;

                    case "^":
                        TextBox2.Text = TextBox2.Text + "^";
                        TextBox2.Text = Convert.ToString(Math.Pow(firstnum, secondnum));

                        break;
                    case "÷":

                        TextBox2.Text = Convert.ToString(firstnum / secondnum);
                        if (secondnum != 0)
                        {
                            result = firstnum / secondnum;
                            TextBox2.Text = result.ToString();
                        }
                        else
                        {
                            TextBox2.Text = "Not Possible";
                        }

                        break;
                }
            }

These commands are declared at the top
namespace WebApplication1
{

    public partial class WebForm3 : System.Web.UI.Page
    {

        String operation = "";
        double firstnum, secondnum;
        private object result;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

When I run the program it opens in the browser right? When I click a number (example: 9) and click the "+" operator and again click another number (example: 8) it does not do anything. It just does not calculate my result. The problem is for all the operators. But when I run the same code in a windows forms app c# the code works perfectly. Pls help.

I have another problem as well:
Clicking one any number and clicking any operator the first number just disappears and the operator does not be visible.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,415 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 51,346 Reputation points
    2021-06-21T14:35:01.46+00:00

    I believe the issue here is with state management. Web apps are stateless. You declare some fields at the top of your page class but fields are only useful for storing data while the page exists. Each request to the server (button click, for example) creates a new page. Hence you cannot store data in fields of a page across requests. The only way to store data in a web app across page requests is to use persistent storage like a database or, more likely, pass the data back to the client and have the client send it back on subsequent requests.

    Note I would recommend you use descriptive control names rather than TextBox2. It makes your code a lot easier to read and we don't have to guess. I'm assuming here that TextBox2 is a readonly control that displays the current value of your calculator.

    When an operator is clicked you capture the current value of TextBox2 into both your first and second operand fields. That is wrong isn't it? Imagine I click the 4 button. Your number handler will store 4 into TextBox2. Then I press the + operator button. You put 4 into both firstnum and secondnum. operator gets the + and then you wipe TextBox2. Now what? Your TextBox2 control which was tracking the current value is gone. You have 3 fields with values in them but the fields won't survive a page request so they are gone the next time a button is clicked.

    The problem, I believe, is that you are assuming the fields will store their values across page requests and that isn't true. To work around this I would recommend you create hidden fields for any values you need to persist across requests that you don't want to show in the UI. I believe at a minimum this would be the current value of the calculator but would probably include the current operator, if any, as well. You could get away with not storing the current value provided TextBox2 always stored the current value but it looks like you use that for input as well. Therefore I would recommend that you create a separate, read only, textbox Results that shows the current value and use TextBox2 only to show the current input as the user presses on number buttons. Each time they press an operator you take the current value of TextBox2, use the operator to combine with Results and then store the value back into Results. You can then wipe TextBox2 for the next set of input. This will allow your data to persist across web requests.

    0 comments No comments