1

I have the following code below:

List<string> group = new List<string>();
List<int> groupInNumber = new List<int>();
Dictionary<string, List<string>> dicMyMap = new Dictionary<string, List<string>>();
Dictionary<string, int[]> a = new Dictionary<string, int[]>();
Dictionary<string, string[]> b = new Dictionary<string, string[]>();

private void SetData(out ExpandableListViewAdapter mAdapter)
    {
        int[] currentStatus = a["currentStatus"];
        string[] statusDesc = b["description"];
        int[] ticketID = a["ticketID"];

        foreach (int s in currentStatus)
        {
            if (s == 1)
            {
                group.Add(statusDesc[0]);
            }
            else if (s == 2)
            {
                group.Add(statusDesc[1]);
            }
            else if (s == 3)
            {
                group.Add(statusDesc[2]);
            }
            else if (s == 4)
            {
                group.Add(statusDesc[3]);
            }
            else if (s == 5)
            {
                group.Add(statusDesc[4]);
            }
            else if (s == 6)
            {
                group.Add(statusDesc[5]);
            }
            else if (s == 7)
            {
                group.Add(statusDesc[6]);
            }
            else if (s == 8)
            {
                group.Add(statusDesc[7]);
            }
            else if (s == 9)
            {
                group.Add(statusDesc[8]);
            }
            else if (s == 10)
            {
                group.Add(statusDesc[9]);
            }
        }

        for (int p = 0; p <= ticketID.Count(); p++)
        {
            groupInNumber.Add(ticketID[p]);
        }

        List<string> ticket = new List<string>();
        for (int z = 0; z <= ticketID.Count(); z++)
        {
            string tix = ticketID[z].ToString();
            ticket.Add(tix);
            dicMyMap.Add(groupInNumber[z].ToString(), ticket);
            ticket.Remove(tix);
        }

        mAdapter = new ExpandableListViewAdapter(this, group, dicMyMap);
    }

The error occurs here, groupInNumber.Add(ticketID[p]); What I don't understand is why the index is outside the bounds when 0 should be referring to the first value inside the array. I tried counting the items in int[] ticketID = a["ticketID"];, isn't null, returned 39 (number of items in the array).

Pam S.
  • 185
  • 1
  • 13

1 Answers1

5

Here is your problem:

for (int p = 0; p <= ticketID.Count(); p++)
{
    groupInNumber.Add(ticketID[p]);
}

It should be:

for (int p = 0; p < ticketID.Count(); p++)

You're iteration goes 1 index too far. 0...9 are 10 items.. so create a for loop to 0..count-1 so it should be p < ticketID.Count()

This also applies to the other for loops


Offtopic:

since ticketID is an Array you also could use the Length property.

for (int p = 0; p < ticketID.Length; p++)

Your groupInNumber is a List<int>. The List class also has an AddRange() method:

So this for loop...

for (int p = 0; p <= ticketID.Count(); p++)
{
    groupInNumber.Add(ticketID[p]);
}

...can be replaced with:

groupInNumber.AddRange(ticketID);
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57