6 Replies - 1136 Views - Last Post: 28 September 2011 - 06:54 PM Rate Topic: -----

#1 bella422316  Icon User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 49
  • Joined: 20-November 09

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.


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

    }



Is This A Good Question/Topic? 0
  • +

Replies To: IF statement in C# ArgumentOutOfRangeException

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

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.
Was This Post Helpful? 0
  • +
  • -

#3 bella422316  Icon User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 49
  • Joined: 20-November 09

Re: IF statement in C# ArgumentOutOfRangeException

Posted 28 September 2011 - 04:40 PM

View PostMomerath, 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
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

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.
Was This Post Helpful? 0
  • +
  • -

#5 bella422316  Icon User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 49
  • Joined: 20-November 09

Re: IF statement in C# ArgumentOutOfRangeException

Posted 28 September 2011 - 05:15 PM

View PostMomerath, 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

Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: IF statement in C# ArgumentOutOfRangeException

Posted 28 September 2011 - 06:45 PM

Works fine for me here, what value are you sending in?
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1