1

Can someone please explain why I'm getting the following error from the following piece of code.

System.ArgumentOutOfRangeException: capacity was less than the current size. (Parameter 'value')

I'm trying to take a semi-colon separated list which is then further comma separated.

The channelCodeToBoxesMapping parameter value expected would be something like this 01,0;02,1;03,1;05,1;06,0;07,1;08,1.

Code:

private bool DetermineWhetherInSingleBoxModeFromChannelCodeMappings(string channelCodeToBoxesMapping)
{
    this.boxes.Clear();
    
    if (string.IsNullOrWhiteSpace(channelCodeToBoxesMapping))
    {
        return false;
    }

    var channelCodeAndBoxPairs = channelCodeToBoxesMapping.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
    foreach (var pair in channelCodeAndBoxPairs)
    {
        var channelCodeAndBoxSplit = pair.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        if (channelCodeAndBoxSplit.Length != 2)
        {
            continue;
        }

        this.boxes.Add((BoxCount.Boxes)Convert.ToInt16(channelCodeAndBoxSplit[1]));
    }

    return this.boxes.Distinct().Count() == 1;
}

Notes:

this.boxes is a list of enums i.e. List<BoxCount.Boxes>.

Bhav
  • 1,957
  • 7
  • 33
  • 66
  • What is the type of `this.boxes`? – Martin Costello Apr 19 '21 at 10:36
  • Does this answer your question? [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Yong Shun Apr 19 '21 at 10:39
  • @MartinCostello It's a list of enums i.e. List – Bhav Apr 19 '21 at 10:42
  • At which line you get this error? – Mohsen Esmailpour Apr 19 '21 at 11:04
  • You are generating 2 string arrays here, can you please tell us the line of code that your stack trace is pinpointing you to? – Yash Gupta Apr 19 '21 at 11:04
  • 1
    Can you `Int16.TryParse(channelCodeAndBoxSplit[1], out var i)` instead of `Convert.ToInt16(channelCodeAndBoxSplit[1])` and see if the conversion succeeds? – mu88 Apr 19 '21 at 11:05
  • @MohsenEsmailpour It's the line with the 'Convert.ToInt16' and the issue is intermittent. – Bhav Apr 19 '21 at 12:36
  • @YashGupta It's the line with the 'Convert.ToInt16'. – Bhav Apr 19 '21 at 12:37
  • @mu88 Any reason why the issue may be intermittent? – Bhav Apr 19 '21 at 12:37
  • 1
    Because sometimes the string isn't a 16-bit integer and sometimes it is? – Martin Costello Apr 19 '21 at 12:56
  • @Bhav You know your data better than me but checking the input for correctness (like `Int16.TryParse()`) and analyzing the results seems better to me than just blindly trusting in the correctness of the input – mu88 Apr 19 '21 at 13:04
  • The input parameter has never changed. – Bhav Apr 19 '21 at 14:13
  • 1
    @Bhav - you might be getting `ArgumentOutOfRangeException` because the `channelCodeAndBoxSplit` array is probably empty (and hence may not contain anything at all at 1th index). Do you mind checking if this string array indeed contains something? – Yash Gupta Apr 19 '21 at 15:09

0 Answers0