7 Replies - 313 Views - Last Post: 18 June 2013 - 08:50 AM Rate Topic: -----

#1 Semus  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 126
  • Joined: 27-May 13

Strange result using TryParse with an if statement

Posted 18 June 2013 - 08:06 AM

The title doesn't really explain the issue very well and I'm sure I'm going to screw this explanation up, but here we go...

Here's my block of code:

            int num1;
            string text;
            Console.Write("Please enter a number between 1 and 10: ");
            text = Console.ReadLine();
            bool isThisANumber = int.TryParse(text, out num1);
            if (isThisANumber == false)
            {
                Console.WriteLine("That is not a number!");
            }
            if (num1 < 1)
            {
                Console.WriteLine("That number is too low!");
            }
            if (num1 > 10)
            {
                Console.WriteLine("That number is too High!");
            }
            if (num1 >= 1 && num1 <= 10)
            {
                Console.WriteLine("That's correct! your number is between 1 and 10!");
            }
            Console.ReadLine();


As you can see, it checks to see if the user entered a number. If they entered a number, it will check to see if it's lower or higher than 1 or 10, respectively. If it's within the boundaries of 1 and 10, it will print out the correct message. However, when you enter a word it will tell you that it's not a number, but it will also tell you that the number is too low because TryParse returned 0. Is there a special way of handling this problem? Keep in mind that if it involves some fancy coding, I might not be able to understand it, but I'll try.

Is This A Good Question/Topic? 0
  • +

Replies To: Strange result using TryParse with an if statement

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,666
  • Joined: 27-December 08

Re: Strange result using TryParse with an if statement

Posted 18 June 2013 - 08:09 AM

You'll want to use else if statements after your first if statement. That way, if the previous condition doesn't hold, check this one. A series of if statements says to check each one.
if(condition){
    //code
}

//equivalent to if(!condition && condition2)
else if(condition2){
   //code
}

else if(condition3){
   //code
}


Was This Post Helpful? 3
  • +
  • -

#3 Semus  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 126
  • Joined: 27-May 13

Re: Strange result using TryParse with an if statement

Posted 18 June 2013 - 08:14 AM

Yup, that fixed it, thanks. I had a feeling it was something simple.

I wasn't using my brain with this one. My first solution would check every single if statement. Your solution only checks the following if statements if the first one is false.

This post has been edited by Semus: 18 June 2013 - 08:16 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,666
  • Joined: 27-December 08

Re: Strange result using TryParse with an if statement

Posted 18 June 2013 - 08:15 AM

Glad I could help!
Was This Post Helpful? 0
  • +
  • -

#5 Linesofcode  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 62
  • Joined: 23-May 13

Re: Strange result using TryParse with an if statement

Posted 18 June 2013 - 08:24 AM

If you have multiple conditions, it would be better the use of Switch(). It's faster and more noticeable.
A great discussion about it: C# switch vs if
Was This Post Helpful? -1
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: Strange result using TryParse with an if statement

Posted 18 June 2013 - 08:37 AM

View PostLinesofcode, on 18 June 2013 - 05:24 PM, said:

If you have multiple conditions, it would be better the use of Switch().


How would this possibly be expressible using switch?
Was This Post Helpful? 0
  • +
  • -

#7 Semus  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 126
  • Joined: 27-May 13

Re: Strange result using TryParse with an if statement

Posted 18 June 2013 - 08:42 AM

View PostLinesofcode, on 18 June 2013 - 08:24 AM, said:

If you have multiple conditions, it would be better the use of Switch(). It's faster and more noticeable.
A great discussion about it: C# switch vs if


Quote

How would this possibly be expressible using switch?


I'm curious as well.

Unfortunately, I'm following challenges from a book that specifically ask me to use if statements. I consider these challenges as practice.

I have learned quite a bit over the last month and I am going back and re-doing every single challenge from the book as sort of a refresher of what I have learned. Each challenge has me use a specific technique to tackle them and I like to stick to that technique for now. At some point I'll go back and introduce my own custom ways of doing them.

This post has been edited by Semus: 18 June 2013 - 08:46 AM

Was This Post Helpful? 0
  • +
  • -

#8 Linesofcode  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 62
  • Joined: 23-May 13

Re: Strange result using TryParse with an if statement

Posted 18 June 2013 - 08:50 AM

In this code it wouldn't be possible, because he has range values. If he would have specific values it would be a better option. I was only giving him that information.

If, for example, he has specific values, he would need only one condition.
if (isThisANumber == false)
{
    Console.WriteLine("That is not a number!");
    return;
}
switch (num1) { 
    case 1: break;
    case 2: break;
    // etc.
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1