0

Working on parsing from a text box to int to get into the incrementHour method as shown.

if (txtHourPlus.Text != String.Empty)
            {
                time1.incrementHour(int.Parse(txtHour.Text));

            }

And in the time class: (the time1 objects class)

 public int incrementHour(int step)
        {
            if (step > 0 && hour < 24)
            {
                //step = step % hour;
                hour = (hour + step) % 24;
                time.AddHours(hour);
                return hour;
            }//end of if

            else
            {
                MessageBox.Show("Please enter a positive number.");

                return 0;
            }//end of else
        }//end of incrementHour

not sure why i'm getting this error. I'm converting it to the corrent data type. Because it accepts an int variable.


Alright well i got it to take the value (small mistake >.> Don't even wanna say it) However as someone already said the method probably needs work because i'm trying to change the Datetime value that i get in the first place and add an hour or subtract and etc etc.

8
  • 2
    txtHour.Text wasn't a valid int. Ta da! Commented Jan 28, 2011 at 22:11
  • Can you post the stack trace? I'm assuming the error is coming from the int.parse. Commented Jan 28, 2011 at 22:11
  • you should prefer to use String.IsNullOrWhite space instead of comparing to String.Empty :) Commented Jan 28, 2011 at 22:13
  • 1
    @Muad'Dib: Have you ever seen TextBox.Text return null? I would be very surprised if that ever happened... and if it did, I think I'd rather find out about it... Commented Jan 28, 2011 at 22:13
  • 2
    Maybe the input string is not in a correct format. Like the error message. Commented Jan 28, 2011 at 22:15

4 Answers 4

3

That will happen if someone has typed "foo" into the text box, for example. You should use int.TryParse instead, which lets you detect the error without an exception being thrown.

I notice that you're not actually using the return value of your method though - just like you're not using the return value of time.AddHours. You are aware that DateTime.AddHours doesn't actually modify the value you're calling it on, aren't you? (I suspect you'll need to tweak that method quite a bit, actually... there are various potential problems with it, depending on exact what you're trying to do.)

(Finally, I'd change the method name to IncrementHour to comply with .NET conventions.)

Sign up to request clarification or add additional context in comments.

2 Comments

int test = 0; bool result = int.TryParse("some string", out test);
>.> wasn't totally aware of that. All i'm trying to do is be able to increment the hours and minutes with this program. I'm just having an issue just doing that.
2

you are testing txtHourPlus for emptiness, but then parsing and passing txtHour. typo (?)

2 Comments

/face palm yeah that was my issue. Now i'm just working on getting the value that i pass to actually change the time.
+1 for the only person who caught the real error not just the bad practices.
1

If your input isn't parsable as an integer, attempting to parse it will raise an exception. Validate or use Int32.TryParse()

Comments

0

Change this part of your code:

if (txtHour.Text != String.Empty)
{
    int parsedValue;

    if (int.TryParse(txtHour.Text, out parsedValue))
    {
        time1.incrementHour(parsedValue);
    }
    else
    {
        // non-numeric value was entered into the textbox - handle accordingly.
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.