2

Alright, nub question. I know. Basic response might be

Convert.ToInt32(string);

But naturally, C# does every natural process to make sure just that doesn't happen.

Here's my code:

            while (true)
            {

                while (true)
                {
                    //ask for time
                    Console.WriteLine("What is the hour?");
                    Console.WriteLine();
                    string s = Console.ReadLine();
                    //convert input to int
                    YourTime.nHour = Convert.ToInt32(s);
                    //check to see if it's legal
                    if ((YourTime.nHour <= 12) || (YourTime.nHour > 0))
                    {
                        break;
                    }
                //etc etc code
                }
            }  

I want to make sure the input is an actual hour. When I run this code, it always labels the if() statement as "true" and breaks, even if I inputted something like -13 or 99.

I'm sure there's a simple replacement for "Convert.ToInt32(s);", but to be honest it seems like I've tried everything. I decided it would be best to follow step-by-step instructions from people who are aware of the code at hand.

[EDIT] - Wrong operator, not the conversion. Thanks to everyone who helped!

Tako M.
  • 295
  • 1
  • 3
  • 13

4 Answers4

6

You need to use AND not OR. So, change it to

if ((YourTime.nHour <= 12) && (YourTime.nHour > 0)) 
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
3

It's your if statement that's invalid, not the Convert.ToInt32

if ((YourTime.nHour <= 12) || (YourTime.nHour > 0)) will always be true. I think you meant to do if ((YourTime.nHour <= 12) && (YourTime.nHour > 0))

pjumble
  • 16,880
  • 6
  • 43
  • 51
2

Do you mean no matter what integer you type in it always breaks?

If so its because no matter what integer you type in it will always pass one of those conditions.

i.e If I entered 10000000, it would still be greater than 0

and if I entered -10000000 it would still be less than 12

IanMelrose
  • 108
  • 7
1

You just mixing two things that should not be mixed. Converter from string to int should not have any business logic incorporated, that is model responsibility to know that this field is actually hours and not payment amount due

So to separate it you can use many things, for example data annotations, take a look at following code

 public class MyTime
 {

 [Require]
 [Range(0, 12, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
 public int Hours { get; set; }

 [Require]
 [Range(0, 59, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
 public int Minutes { get; set; }

 }

this way you have model defined which can be validated against rules it knows about and you can get error message that has sense

PS this link can show you how to create custom validator if you are using data annotations outside asp.net mvc

vittore
  • 17,449
  • 6
  • 44
  • 82
  • also check this link http://stackoverflow.com/questions/3699038/entity-framework-validation-server-clientside-jquery-with-data-annotation – vittore Jan 30 '12 at 02:51
  • Minor adjustment... minutes go from 0 to 59... 60 is actually minute 0 of the next hour :) – DRapp Jan 30 '12 at 03:06
  • It's like you're speaking Japanese, man! I don't know what any of that means, and I'm tempted to ask if it's even C#. :P – Tako M. Jan 30 '12 at 20:40