Click here to Skip to main content
15,914,795 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Help me..........

Im a beginner to c#.net. Im trying simple calculator program with one textbox and remainig operations like add,sub are in buttons. My problem is

C#
int total1=0;
        int total2=0;
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            total1 = total1 + int.Parse(txtAns.Text);
            total1 =Convert .ToInt32 (txtAns .Text .ToString ());
            txtAns.Text = string.Empty;
}
 protected void  btnEqual_Click(object sender, EventArgs e)
{
total1 = Convert.ToInt32(txtAns.Text);
            total2 = total1 + int.Parse(txtAns.Text);
            txtAns.Text = total2.ToString();
}


While debug in this code, when add button is clicked, i'm getting first value. But when clicked on equal button, i cannot get that first value to add, only the second value getting double....i don't know whats wrong in my code. Anybody please guide me....
Posted
Updated 25-Mar-12 21:31pm
v2

It's difficult to tell what your code is meant to do, what it does is tries to confuse at every step...
Some rules for you:

1) If something is a string, there is no point in calling ToString on it - it won't do anything useful. TextBox.Text is a string already - leave it alone!
2) Pick a method to convert from string to int, and stick with it. Using two different methods that do the same thing, is pointless and confusing.
3) Don't write code that undoes the previous line with the next - there is no point is assigning a value to total1 in one line, if you are going to overwrite it in the next.

So, your code, tidied up a bit, and with redundant code removed:
C#
int total1 = 0;
int total2 = 0;
protected void btnAdd_Click(object sender, EventArgs e)
    {
    total1 = int.Parse(txtAns.Text);
    txtAns.Text = string.Empty;
    }
protected void btnEqual_Click(object sender, EventArgs e)
    {
    total1 = int.Parse(txtAns.Text);
    total2 = total1 + int.Parse(txtAns.Text);
    txtAns.Text = total2.ToString();
    }

However, I don't think that this does what you actually want to to do.
I think what you want to do is keep a running total, and add to it each time, somehow. But I'm not sure exactly what you are trying to do. Care to explain?
 
Share this answer
 
Comments
Priyaaammu 27-Mar-12 2:29am    
Thanks for your reply........Actually my task is to a simple calculator program. Im having only one text box to get values and to display result. When searched for related programs,i got the code for windows application so i tried that for my web application...

When debug on above code,i got first value in total1...but in equal event,total1 value will be zero only. I didnt get what is my fault,guide me.......
OriginalGriff 27-Mar-12 3:29am    
Ah. "Web application" There is your problem...

When you use a Web application, you can't use variables in the same way that you do in a Windows app - the application terminates when the page is completely loaded, and re-started when your user posts back. So every time they press a button, your variables are reset to the original state (i.e. 0) and you work on from there.

If you want to preserve them, then there are two things you can do:
1) Use one of the several "preservation" techniques for web apps. These include Sessions, Cookies, Parameters, and Databases. For the moment, these are probably a little heavy for what you want.
2) Keep a "running total" label or textbox on your page. This way both your user can see the total at all times, and you can access it to add a new value. I would probably use a Read-only Textbox, myself.
Priyaaammu 27-Mar-12 5:05am    
Thanks a lot.....i used view state management and solved my problem. And i have one more doubt in postback. Actually why we are using postback? Every time client sends request and server sending response is a regular thing...then what is the need of postback? Guide with simple example...
OriginalGriff 27-Mar-12 5:16am    
A Postback indicates that this is not the first time a page has been requested - that the fields should contain user information and you should not default them.
Priyaaammu 27-Mar-12 6:32am    
can't get...what is the use of it,simple example please.....
I thought your problem is that txtAns.Text = string.Empty in the add-button method.
 
Share this answer
 
Comments
Priyaaammu 27-Mar-12 2:32am    
Im using that line to clear the first entered value in text box when "+" button is clicked because im having only one textbox to get and display values

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900