Error using While loop. Assign: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

I am getting this error in while loop which has a very simple condition. I just want to loop until the value is less than the variable defined.
My condition has an increment variable which value is assigned to 0 and as it loops through the value is incremented and the other value is the count/length of a dictionary element. The type of dictionary is divisionsDictionary(String, IList(of String)) and the condition is:

while(counterSubDivArr < divisionsDictionary(divisionNames(i)).Count)

Little bit confused with the description
Can you explain again

@Khadija_Zeb,

Check if your divisionNames have index number your while loop is trying to access

Index starts with zero to your divisionNames.Count -1

Thanks,
Ashok :slightly_smiling_face:

divisionNames has 3 elements so it is returning 2 after subtracting 1 which is alright. And the value of “i” is 0 which keeps incrementing

@Khadija_Zeb,

That’s right your while loop will start with i=0 then it will increment to 1 and then 2 that’s it.

Kindly close the thread if it’s clear by marking my answer as solution.

Thanks,
Ashok :slight_smile:

But it is still giving the error, I know my arrays have elements but I don’t know why it’s giving the error

@Khadija_Zeb,

Try logging the array values before While loop using this expression in Log Message activity.

String.Join(",",divisionNames)

I suspect the array have some less or no items.

As you can see, divsionNames array has elements

@Khadija_Zeb,

Ok value’s seems fine. Can you share screenshot of the While loop how you have implemented and what you are trying to do with. May be there could be another approach to achieve this.