Hi I am pretty new to coding especially in c# and have a problem with my project. I am looking to make a calculator online using c# and upload it to a database and host in azure. Here is my question:
I am having a problem with my c# code in Visual Studio using web forms. It is simply not working, It can input numbers and operations however does not get the correct result eg 3 + 3 = 33. This is a conversion from WinApp, so it may be from there? But I re-created the UI and repurposed the code to fit a online app. After I get this to work I plan on uploading it to azure. Is there any reason why this is not working? My WinApp in .NET has a very similar code and works so is it a .NET/ASP.net issue? Any help is appreciated!
Here is the .aspx.cs file:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class WebForm1 : Page
{
bool operationPerf = false;
string operation = "";
double answer = 0;
protected void NumbEvent(object sender, EventArgs e)
{
if (textbox.Text == "0" || operationPerf)
textbox.Text = string.Empty;
Button butt = (Button)sender;
textbox.Text += butt.Text;
operationPerf = false;
label.Text = label.Text + " " + textbox.Text;
}
protected void OperandEvent(object sender, EventArgs e)
{
operationPerf = true;
Button butt = (Button)sender;
string newOperand = butt.Text;
label.Text = label.Text + " " + newOperand;
switch (operation)
{
case "+":
textbox.Text = (answer + Double.Parse(textbox.Text)).ToString();
break;
case "-":
textbox.Text = (answer - Double.Parse(textbox.Text)).ToString();
break;
case "*":
textbox.Text = (answer * Double.Parse(textbox.Text)).ToString();
break;
case "/":
textbox.Text = (answer / Double.Parse(textbox.Text)).ToString();
break;
case "^":
textbox.Text = (Math.Pow(answer, Double.Parse(textbox.Text))).ToString();
break;
case "√":
textbox.Text = (Math.Sqrt(Double.Parse(textbox.Text))).ToString();
break;
default:
break;
}
answer = Double.Parse(textbox.Text);
operation = newOperand;
}
protected void Bequal_Click(object sender, EventArgs e)
{
operationPerf = true;
switch (operation)
{
case "+":
textbox.Text = (answer + Double.Parse(textbox.Text)).ToString();
break;
case "-":
textbox.Text = (answer - Double.Parse(textbox.Text)).ToString();
break;
case "*":
textbox.Text = (answer * Double.Parse(textbox.Text)).ToString();
break;
case "/":
textbox.Text = (answer / Double.Parse(textbox.Text)).ToString();
break;
case "^":
textbox.Text = (Math.Pow(answer, Double.Parse(textbox.Text))).ToString();
break;
case "√":
textbox.Text = (Math.Sqrt(Double.Parse(textbox.Text))).ToString();
break;
default:
break;
}
label.Text = label.Text + " = " + textbox.Text;
label.Text = "";
answer = Double.Parse(textbox.Text);
textbox.Text = answer.ToString();
answer = 0;
operation = "";
}
protected void BC_Click(object sender, EventArgs e)
{
textbox.Text = "0";
label.Text = "";
answer = 0;
operation = "";
}
}
}
Here is the .aspx file:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Calculator 9001</title>
</head>
<body>
<form id="form1" runat="server">
<p>
<asp:Label ID="label" runat="server"></asp:Label>
</p>
<p>
<asp:TextBox ID="textbox" runat="server" Height="35px" ReadOnly="True" Width="300px"></asp:TextBox>
</p>
<p>
</p>
<p>
<asp:Button ID="Bdiv" runat="server" Height="75px" OnClick="OperandEvent" Text="/" Width="100px" />
<asp:Button ID="Bmul" runat="server" Height="75px" OnClick="OperandEvent" Text="*" Width="100px" />
<asp:Button ID="Bmin" runat="server" Height="75px" OnClick="OperandEvent" Text="-" Width="100px" />
<asp:Button ID="Bplus" runat="server" Height="75px" OnClick="OperandEvent" Text="+" Width="101px" />
</p>
<p>
<asp:Button ID="B7" runat="server" Height="75px" OnClick="NumbEvent" Text="7" Width="100px" />
<asp:Button ID="B8" runat="server" Height="75px" OnClick="NumbEvent" Text="8" Width="100px" />
<asp:Button ID="B9" runat="server" Height="75px" OnClick="NumbEvent" Text="9" Width="100px" />
<asp:Button ID="Bpow" runat="server" Height="75px" OnClick="OperandEvent" Text="^" Width="100px" />
</p>
<p>
<asp:Button ID="B4" runat="server" Height="75px" OnClick="NumbEvent" Text="4" Width="100px" />
<asp:Button ID="B5" runat="server" Height="75px" OnClick="NumbEvent" Text="5" Width="100px" />
<asp:Button ID="B6" runat="server" Height="75px" OnClick="NumbEvent" Text="6" Width="100px" />
<asp:Button ID="Broot" runat="server" Height="75px" OnClick="OperandEvent" Text="√" Width="100px" />
</p>
<p>
<asp:Button ID="B1" runat="server" Height="75px" OnClick="NumbEvent" Text="1" Width="100px" />
<asp:Button ID="B2" runat="server" Height="75px" OnClick="NumbEvent" Text="2" Width="100px" />
<asp:Button ID="B3" runat="server" Height="75px" OnClick="NumbEvent" Text="3" Width="100px" />
</p>
<p>
<asp:Button ID="B0" runat="server" Height="75px" OnClick="NumbEvent" Text="0" Width="100px" />
<asp:Button ID="Bdot" runat="server" Height="75px" OnClick="NumbEvent" Text="." Width="100px" />
<asp:Button ID="BC" runat="server" Height="75px" OnClick="BC_Click" Text="C" Width="100px" />
<asp:Button ID="Bequal" runat="server" Height="75px" OnClick="Bequal_Click" Text="=" Width="100px" />
</p>
</form>
</body>
</html>
I think it's these page-level variables.
bool operationPerf = false;
string operation = "";
double answer = 0;
It's like @erastl was saying. Those vars will not hold their values. You need a hidden field on the page, or a viewstate var.
If I'm not mistaken, ViewState and SessionState vars are always strings, so you have to cast them when you use them. Edit: They are objects. Casting still required.
protected void Page_Load(object sender, EventArgs e)
{
ViewState["operationPerf"] = "false";
ViewState["operation"] = string.Empty;
ViewState["answer"] = "0";
}
Later
if (textbox.Text == "0" || bool.Parse(ViewState["operationPerf"].ToString()) == true)
...
And instead of
answer = Double.Parse(textbox.Text);
Use
ViewState["answer"] = textbox.Text;
Use Double.Parse
when you actually use the value for a calculation.
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.