Let's create a simple ASP.NET web application with a class that represents a basic calculator.
File > New > Project
.ASP.NET Web Application
.Create
.In the Solution Explorer, right-click on your project name, select Add > Class
.
Name the class file Calculator.cs
and replace its contents with the following code:
Example
using System;
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public int Subtract(int a, int b)
{
return a - b;
}
public int Multiply(int a, int b)
{
return a * b;
}
public double Divide(int a, int b)
{
if (b == 0)
{
throw new DivideByZeroException("Cannot divide by zero.");
}
return (double)a / b;
}
}
This class contains methods for basic arithmetic operations: addition, subtraction, multiplication, and division.
Add > Web Form
.Calculator.aspx
.Calculator.aspx
and replace its contents with the following code:Example
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Calculator.aspx.cs" Inherits="YourNamespace.Calculator" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<meta charset="utf-8" />
<title>Calculator</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Simple Calculator</h2>
<p>
Enter two numbers:
<asp:TextBox ID="txtNum1" runat="server"></asp:TextBox>
<asp:TextBox ID="txtNum2" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
<asp:Button ID="btnSubtract" runat="server" Text="Subtract" OnClick="btnSubtract_Click" />
<asp:Button ID="btnMultiply" runat="server" Text="Multiply" OnClick="btnMultiply_Click" />
<asp:Button ID="btnDivide" runat="server" Text="Divide" OnClick="btnDivide_Click" />
</p>
<asp:Label ID="lblResult" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Add > New Item
.Web Form
and name it Calculator.aspx.cs
.Example
using System;
namespace YourNamespace
{
public partial class Calculator : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Optional: code to execute when the page loads
}
protected void btnAdd_Click(object sender, EventArgs e)
{
Calculate('+');
}
protected void btnSubtract_Click(object sender, EventArgs e)
{
Calculate('-');
}
protected void btnMultiply_Click(object sender, EventArgs e)
{
Calculate('*');
}
protected void btnDivide_Click(object sender, EventArgs e)
{
Calculate('/');
}
private void Calculate(char operation)
{
try
{
int num1 = int.Parse(txtNum1.Text);
int num2 = int.Parse(txtNum2.Text);
Calculator calc = new Calculator();
double result;
switch (operation)
{
case '+':
result = calc.Add(num1, num2);
break;
case '-':
result = calc.Subtract(num1, num2);
break;
case '*':
result = calc.Multiply(num1, num2);
break;
case '/':
result = calc.Divide(num1, num2);
break;
default:
throw new InvalidOperationException("Invalid operation.");
}
lblResult.Text = $"Result: {result}";
}
catch (Exception ex)
{
lblResult.Text = $"Error: {ex.Message}";
}
}
}
}
Build the solution and run the application. You'll see a web form with textboxes to input two numbers and buttons to perform addition, subtraction, multiplication, and division. When you click on any operation button, the result will be displayed below.
This example demonstrates the usage of a simple class (Calculator
) within an ASP.NET web application to perform basic arithmetic operations. You can extend this example by adding more functionality or enhancing the user interface.