Reputation: 1
So, I'm begining ASP.NET, and at the same time learning a small bit of C#, enough to get by making web pages. I just took on the project of making a basic visual calculator, the kind with the fancy buttons that you click. This is my C# 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 ExtraCredit_GraphicCalculator : System.Web.UI.Page
{
string number1 = "";
string number2 = "";
bool EnteringNumber1 = true;
bool EnteringNumber2 = false;
protected void Page_Load(object sender, EventArgs e)
{
while (EnteringNumber1)
{
Enter9.Click += (s, args) =>
{
number1 = number1 + "9";
};
Enter8.Click += (s, args) =>
{
number1 = number1 + "8";
};
Enter7.Click += (s, args) =>
{
number1 = number1 + "7";
};
Enter6.Click += (s, args) =>
{
number1 = number1 + "6";
};
Enter5.Click += (s, args) =>
{
number1 = number1 + "5";
};
Enter4.Click += (s, args) =>
{
number1 = number1 + "4";
};
Enter3.Click += (s, args) =>
{
number1 = number1 + "3";
};
Enter2.Click += (s, args) =>
{
number1 = number1 + "2";
};
Enter1.Click += (s, args) =>
{
number1 = number1 + "1";
};
Enter0.Click += (s, args) =>
{
number1 = number1 + "0";
};
Result.Text = number1;
}
}
}
Just a few things to notice, these Event#.Something each correlate to a number button, so with Enter7.Click, on the click of the button labeled 7, it does something. At the end, the Result.Text correlates to a Label that displays the result. Right now, I just want to be able to see the results of each button click, but I plan to add functions for a second number and math functions like exponents and square roots.
What exactly can I do to fix this error?
Upvotes: 0
Views: 823
Reputation: 5264
First of all EnteringNumber1
is always true so your Page_Load
method never exits. It only continues to add event handlers which is probably causing your memory error. Know that Page_Load
is an event that fires during the initialization portion of the page. You'll typically use this method to set properties in controls and to establish database connections (see Page Life Cycle Overview for more info).
Remember HTML is static, and all asp.net is doing is creating these static html pages on the server. You'll want to remove the infinite loop and let the events register once.
You'll also want to handle some sort of displaying of the value in the click events.
In this scenario, I really don't see much happening in your Page_Load
handler. You can create separate event handlers for each button if you want (you could simplify this later). By default, when the user click's a button on the page, a PostBack to the server happens and the server does some calculations and a new version of the page is returned. In the end, you would have something that looks kind of like the following:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack()) // we only want this stuff to happen on initial load
{
hiddenField.value = 0;
}
}
protected void Enter9_OnClick(object sender, EventArgs e)
{
// a postback to the server just happened
hiddenField.value = (double)hiddenField.value + 9;
}
....
....
Note that this code isn't tested and may not be 100% correct, but it gets you the basic idea on how asp.net works.
Now when you're comfortable with page lifecycle what you REALLY want to do is a) handle the calculations client side in javascript or b) marshall the calls from the client side code to the server (where your computations will happen) and back using simple ajax. Check out jQuery to help with the client side.
Upvotes: 3
Reputation: 616
One big error that I see is that you never set EnteringNumber1 to false, so upon page load you have an infinite loop, and in that infinite loop you are registering event handlers for your buttons repeatedly, which keeps allocating memory.
Dump the loop. There shouldn't be any need for it. Make sure your result also goes to an input field in your asp so that when another button is pressed, you can "add" the number to the number you've already established.
Upvotes: 3