Tako M.
Tako M.

Reputation: 295

Converting Strings to Ints

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!

Upvotes: 2

Views: 240

Answers (4)

vittore
vittore

Reputation: 17579

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

Upvotes: 1

IanMelrose
IanMelrose

Reputation: 108

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

Upvotes: 2

pjumble
pjumble

Reputation: 16960

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))

Upvotes: 3

D&#39;Arcy Rittich
D&#39;Arcy Rittich

Reputation: 171411

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

if ((YourTime.nHour <= 12) && (YourTime.nHour > 0)) 

Upvotes: 6

Related Questions