public void setSecond(int s)
{
int seconds = (s >= 0 && s < 60) ? s : 0;
totalSeconds = (getHour() * 3600) + (getMinute() * 60) + seconds;
if (s >= 0 && s < 60)
seconds = s;
else
throw new ArgumentOutOfRangeException(
"Second", s, "Second must be 0-59");
}
IF statement in C# ArgumentOutOfRangeException
Page 1 of 16 Replies - 3045 Views - Last Post: 28 September 2011 - 06:54 PM
#1
IF statement in C# ArgumentOutOfRangeException
Posted 28 September 2011 - 04:05 PM
Hello All I new to C# and having been working on a tutorial for a couple hours however I am having a little bit of trouble with my if statement I can not seem to figure out how to properly address the ArgumentOutOfRangeException in my if statement. Any help would be greatly appreciated.
Replies To: IF statement in C# ArgumentOutOfRangeException
#2
Re: IF statement in C# ArgumentOutOfRangeException
Posted 28 September 2011 - 04:24 PM
Address it in what way? The code looks fine except that it doesn't do anything as you don't return a value or output a value or store a value.
#3
Re: IF statement in C# ArgumentOutOfRangeException
Posted 28 September 2011 - 04:40 PM
Momerath, on 29 September 2011 - 12:24 AM, said:
Address it in what way? The code looks fine except that it doesn't do anything as you don't return a value or output a value or store a value.
Thanks for the reply I know the code works. However I want to properly write a throw new ArgumentOutOfRangeException if seconds were more than 60 like how exactly would the if statement look
#4
Re: IF statement in C# ArgumentOutOfRangeException
Posted 28 September 2011 - 05:00 PM
Exactly like you have it. Still not sure what you are asking. BTW, line 3 ensures that you will never throw the exception.
#5
Re: IF statement in C# ArgumentOutOfRangeException
Posted 28 September 2011 - 05:15 PM
Momerath, on 29 September 2011 - 01:00 AM, said:
Exactly like you have it. Still not sure what you are asking. BTW, line 3 ensures that you will never throw the exception.
Even when I removed the third line I am still not able to throw the exception
I am missing something in my code
This post has been edited by bella422316: 28 September 2011 - 05:16 PM
#6
Re: IF statement in C# ArgumentOutOfRangeException
Posted 28 September 2011 - 06:45 PM
Works fine for me here, what value are you sending in?
#7
Re: IF statement in C# ArgumentOutOfRangeException
Posted 28 September 2011 - 06:54 PM
Shouldn't the check be the first thing you do? If you are checking to see if the argument is correct, then you should do that before you start using it's data.
Page 1 of 1

New Topic/Question
Reply



MultiQuote


|