surfasb
surfasb

Reputation: 968

ambiguity in loops?

The error I'm getting is "System.ArgumentOutOfRangeException was unhandled." It asks that month be between 1 and 12. But look at the variable in the debugger says it equals to 1, along with a Debug.Writeline.

    int month, year, total;
    total = 0;
    DateTime dayToFind;

    for (year = 1001; year < 1201; year++){
        for (month = 1; month < 12; month++){
            dayToFind = new DateTime(year, month, DateTime.DaysInMonth(month, year));
            // The error points at the last occurance of month above.
            total = (dayToFind.DayOfWeek == DayOfWeek.Monday) ? 1 : 0;
        }

    }

Upvotes: 0

Views: 89

Answers (2)

Hans Passant
Hans Passant

Reputation: 941525

You reversed the arguments for the DaysInMonth method. Year goes first.

Upvotes: 2

user596075
user596075

Reputation:

You have your DateTime.DaysInMonth() call backwards. Change it to this:

DateTime.DaysInMonth(year, month)

When you put the year variable in the place of month, it was greater than the max that it can be (greater than 12), resulting in the ArgumentOutOfRangeException.

DateTime.DaysInMonth()

Upvotes: 8

Related Questions