1

I keep getting this 'ArgumentOutOfRange exception non-negative number required, Parameter name: index', whenever I try to open the page and I can't seem to figure out where/how exactly the negative number is appearing. Thank you all in advance!!

var months = data.OrderBy(x => x.ApproximatedStartDate).Select(x => x.Month).Distinct((x, y) => x == y).OrderBy(x => x).ToList();
var upcomingMonths = months.GetRange(months.IndexOf(DateTime.Today.Month), months.Count - months.IndexOf(DateTime.Today.Month));

I'm getting the exception when the code reads the 'upcomingMonths' variable.

stack trace:

[ArgumentOutOfRangeException: Non-negative number required. Parameter name: index]
System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) +72
System.Collections.Generic.List`1.GetRange(Int32 index, Int32 count) +4951591
InitializeChartBC() 
Page_Load(Object sender, EventArgs e)
System.Web.UI.Control.OnLoad(EventArgs e) +103
System.Web.UI.Control.LoadRecursive() +68
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3811
5
  • Please add the stacktrace. Commented Nov 13, 2017 at 15:34
  • 3
    What happens if months is empty? Commented Nov 13, 2017 at 15:34
  • Why not "Where( Month >= DateTime.Today.Month)" (pseudocode) ? Commented Nov 13, 2017 at 15:42
  • 1
    A minimal reproducible example would be helpful here. It's not really clear what you're trying to achieve, or what data you have. Commented Nov 13, 2017 at 15:43
  • Well the exception tells you that it is the "index" argument. The stack trace shows that it is the GetRange method of List. Given you only use this once in your quoted code then you can conclude that months.IndexOf(DateTime.Today.Month) must be returning a negative number. The documentation of IndexOf will tell you exactly when that will return a negative number. Job done. Learning to read exception information is a vital skill while programming and one you should definitely take the time to learn. Commented Nov 13, 2017 at 15:48

1 Answer 1

3

According to Documentation header of the List

// Exceptions:
//   T:System.ArgumentOutOfRangeException:
//     index is less than 0.-or-count is less than 0.

So I think months doesn't contain Current Month. Before you call the months.GetRange check whether it contains current month and then call the GetRange.

var months = data.OrderBy(x => x.ApproximatedStartDate).Select(x => x.Month).Distinct((x, y) => x == y).OrderBy(x => x).ToList();
 //Anyone corrent me as the list is converted to **.ToList** it wont throw null error I feel
List<T> upcomingMonths = null;  //Where T is the type of the list
if(months.IndexOf(DateTime.Today.Month)>=0)
      upcomingMonths = months.GetRange(months.IndexOf(DateTime.Today.Month), months.Count - months.IndexOf(DateTime.Today.Month));
Sign up to request clarification or add additional context in comments.

14 Comments

Bingo! That's the same conclusion I reached.
If it doesn't contain current month, how exactly would I add that into it before calling GetRange?
What about using a "Where" instead of "GetRange"?
Thank you for the example! I'm not too familiar with checking indexes just yet, how would you go about checking if the index is a negative number, like you said? @john
@Fildor he wants the list starting at index from where the month starts until the end of the list. If the Data is started then he can check month at-least or at-most using where condition
|

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.