c#, beginner questions.
Page 1 of 1
CraweN




Posts: 719
Location: Upside down in chair
PostPosted: Sun, 31st May 2009 12:40    Post subject: c#, beginner questions.
Hey people.

Just for fun I've begun trying to learn how to program. Currently I'm reading "Microsoft Visual C-Sharp 2008 Step by Step" by John Sharp.
So I've installed Microsoft Visual Studio 2008, read some of the chapters and made all of the exercises so far, which has been great fun.
So I decided to create my own Testzone project, which would contain all the things I've supposedly learned so far.

I want to create a mini-calculator which can add, substract, multiply and devide. So I have created a textbox which contains the results, a textbox which contains the number I want to result(a number which will be added, substracted etc.. to the result). but as you can probably guess I'm having a bit of a problem getting the fucker working.

here is the button code for the add button which should take the string value from the txtCAOBox.Text and add it to the value which is currently in the txtResults.Text.

So what am I doing wrong.



Code:

private void btnResultAdd_Click(object sender, RoutedEventArgs e)
        {
            if (txtResult.Text != null)
            {
                txtResult.Text = "";
            }
           
            int tempResult = 0;
            tempResult = int.Parse(txtCAObox.Text);
            tempResult += int.Parse(txtResult.Text);
            txtResult.Text = tempResult.ToString();
           
               
        }


Hardware: Ryzen 3700x, B450 MSI Gaming Pro carbon AC, GTX1080, 32 GB 3200 Mhz cas 14, 256 EVO SSD, 1 TB EVO SSD and 4 TB HDD.
Console: xbox, wii and xbox 360
Back to top
Rinze
Site Admin



Posts: 2343

PostPosted: Sun, 31st May 2009 14:57    Post subject:
Why do you set the result to "" if it already contains a number?
Back to top
me7




Posts: 3936

PostPosted: Sun, 31st May 2009 16:07    Post subject:
You do the following: you check if txtResult is not empty [ if (txtResult.Text != null) ] and clear it in that case. That way [ tempResult += int.Parse(txtResult.Text); ] can never work, since txtResult.Text is always either null or contains an empty String.

Maybe this is what you intended to do:
Code:
private void btnResultAdd_Click(object sender, RoutedEventArgs e)
     {
            if (txtResult.Text != null)
            {
            int tempResult = 0;
            tempResult = int.Parse(txtCAObox.Text);
            tempResult += int.Parse(txtResult.Text);
            txtResult.Text = tempResult.ToString();
           }
               
      }
Back to top
lhzr




Posts: 3902
Location: RO
PostPosted: Sun, 31st May 2009 17:24    Post subject:
yeah, that looks alright, but you should check for txtResult.Text not being empty, instead of null.
Back to top
VGAdeadcafe




Posts: 22230
Location: ★ ಠ_ಠ ★
PostPosted: Sun, 31st May 2009 18:23    Post subject: Re: c#, beginner questions.
CraweN wrote:
Currently I'm reading "Microsoft Visual C-Sharp 2008 Step by Step" by John Sharp.

Laughing
Back to top
lhzr




Posts: 3902
Location: RO
PostPosted: Sun, 31st May 2009 19:57    Post subject:
ahaha, didn't notice that Laughing
Back to top
CraweN




Posts: 719
Location: Upside down in chair
PostPosted: Sun, 31st May 2009 20:37    Post subject:
me7 wrote:
You do the following: you check if txtResult is not empty [ if (txtResult.Text != null) ] and clear it in that case. That way [ tempResult += int.Parse(txtResult.Text); ] can never work, since txtResult.Text is always either null or contains an empty String.

Maybe this is what you intended to do:
Code:
private void btnResultAdd_Click(object sender, RoutedEventArgs e)
     {
            if (txtResult.Text != null)
            {
            int tempResult = 0;
            tempResult = int.Parse(txtCAObox.Text);
            tempResult += int.Parse(txtResult.Text);
            txtResult.Text = tempResult.ToString();
           }


}




hey peps.

the reason for the if loop is me trying to find why the program fails. I wanted to see if it was because the string hadn't been declared(which it has).

When I run the window appears. I enter a number into the txtCAOBox textbox and press the add button. The program crashes and highlights this line of the code, and displays this error message. " Input string was not in a correct format "



Code:

private void btnResultAdd_Click(object sender, RoutedEventArgs e)
        {
            int tempResult = 0;
            tempResult = int.Parse(txtCAObox.Text);
     --> tempResult += int.Parse(txtResult.Text); <--
            txtResult.Text = tempResult.ToString();


                 
        }


Hardware: Ryzen 3700x, B450 MSI Gaming Pro carbon AC, GTX1080, 32 GB 3200 Mhz cas 14, 256 EVO SSD, 1 TB EVO SSD and 4 TB HDD.
Console: xbox, wii and xbox 360
Back to top
me7




Posts: 3936

PostPosted: Sun, 31st May 2009 20:50    Post subject:
Simple: you can't parse an empty String to integer.
try this:

Code:
private void btnResultAdd_Click(object sender, RoutedEventArgs e)
     {
            if (txtResult.Text.equals(""))
            {
             txtResult.Text = "0";
            }
            int tempResult = 0;
            tempResult = int.Parse(txtCAObox.Text);
            tempResult += int.Parse(txtResult.Text);
            txtResult.Text = tempResult.ToString();
     }
Back to top
CraweN




Posts: 719
Location: Upside down in chair
PostPosted: Sun, 31st May 2009 21:14    Post subject:
me7 wrote:
Simple: you can't parse an empty String to integer.
try this:

Code:
private void btnResultAdd_Click(object sender, RoutedEventArgs e)
     {
            if (txtResult.Text.equals(""))
            {
             txtResult.Text = "0";
            }
            int tempResult = 0;
            tempResult = int.Parse(txtCAObox.Text);
            tempResult += int.Parse(txtResult.Text);
            txtResult.Text = tempResult.ToString();
     }


Thanks! Worked like a charm. I'll keep that in mind for the next projects.


Hardware: Ryzen 3700x, B450 MSI Gaming Pro carbon AC, GTX1080, 32 GB 3200 Mhz cas 14, 256 EVO SSD, 1 TB EVO SSD and 4 TB HDD.
Console: xbox, wii and xbox 360
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group