Web application variable not setting help, This is me requesting help, not me showing how to fix this problem

I'm trying to make a web application that will allow the user to to basic calculator functions. I have four buttons, addition, subtraction, ect, and 2 text boxes, a input text box and output text box. I have it so when a button is clicked it runs the calculate method and passes the method the appropriate enumeration of CalculationType (except for the addition button). The problem i'm having is that I'd expect for x to be set to what ever it is set to at the end of the calculate method. But even though i use x to set the outputTextBox's text, outside of the method x is still 0, and if i execute the method again x is still set to 0. Does anyone know how to fix this, or whats going on? Because I'm pretty sure if this was a windows form application this would work.

—–start of code —–

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;

public partial class Calculator : System.Web.UI.Page { double x; double holder; enum CalculationType { addition = 1, subtraction =2, multiplication =3, division =4 } protected void Page_Load(object sender, EventArgs e) { outputTextBox.Text = "0"; errorLabel.Text = ""; } protected void addButton_Click(object sender, EventArgs e) { //this is just to make sure that its not the calculate method x = x + 5; outputTextBox.Text = x.ToString(); } private void calculate(CalculationType type) {

 errorLabel.Text = ""; holder = Convert.ToDouble(inputTextBox.Text); switch (type) { case CalculationType.addition: x = x + holder; break; case CalculationType.subtraction: x = x - holder; break; case CalculationType.multiplication: x = x * holder; break; case CalculationType.division: x = x / holder; break; } outputTextBox.Text = x.ToString(); } private void updateDispaly() { } protected void subButton_Click(object sender, EventArgs e) { calculate(CalculationType.subtraction); } protected void divButton_Click(object sender, EventArgs e) { calculate(CalculationType.division); } protected void multiButton_Click(object sender, EventArgs e) { calculate(CalculationType.multiplication); } 


—–end of code—–

by PizzaIsMyFuel via /r/csharp

Leave a Reply