Question Out of range ArrayList

mrojas

Member
Joined
May 4, 2009
Messages
11
Location
Concord, Ca.
Programming Experience
5-10
I'm trying to populate a combo box with unique file name extensions found in a given directory
I create an ArrayList to store directory files' names. I know that this array may have file names with duplicate file name extentions.
I traverse the given directory inserting extension names in array elements
I pass the now filled ArrayList to a function. This function removes duplicates. It does so the first time the application is run at load time without any error messages. Subsequent execution of this function, after the user selects a folder, it generates the following error message:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
 

Attachments

  • RemoveDuplicates.txt
    1.1 KB · Views: 25
you are using For i = 0 to... Step -1

and you are using arrNoDuplicates(i - 1) inside the For Loop..

This means eventually you are looking for the array item (-1) which doesn't exist..

Try for i = 1 to.....
 
Thanks for your quick reply.
I've tried your suggestion and used it with a directory in which there are only two files.
The array count correctly reports the count of 1, two elements, 0 and 1.
I get the same error message.
 
i almost forgot, you are using for i = 1 to.. but then you are using step -1

Use For i = intcount to 1..

you are supposed to be stepping backwards, not forwards..

Apologies for not spotting it sooner.. shouldn't have replied when I was distracted
 
Here is a quick write-up using your code with the adjustments if it helps.

Module Module1

   Dim arrWithDuplicates As New ArrayList
 
    Sub Main()

      arrWithDuplicates.Add("1")
      arrWithDuplicates.Add("2")
      arrWithDuplicates.Add("2")
      arrWithDuplicates.Add("3")
      arrWithDuplicates.Add("3")
      arrWithDuplicates.Add("4")
      arrWithDuplicates.Add("5")
      arrWithDuplicates.Add("6")
      arrWithDuplicates.Add("7")
      arrWithDuplicates.Add("7")


      Console.WriteLine("Here is the original list")
      For i = 0 To arrWithDuplicates.Count - 1
         Console.WriteLine(arrWithDuplicates(i))
      Next

      
      Dim arrNoDuplicates As New ArrayList
      arrNoDuplicates = f_RemoveArrayDuplicates(arrWithDuplicates)


Console.WriteLine()
      Console.WriteLine("Here is the List without Duplicates")
      For i = 0 To arrNoDuplicates.Count - 1
         Console.WriteLine(arrNoDuplicates(i))
      Next


   End Sub

    Private Function f_RemoveArrayDuplicates(arrWithDuplicates As ArrayList) As ArrayList
      Dim i As Integer
      Dim intCount As Integer
      'TODO: After the Browse button has been clicked and a new folder has been chosen,
      ' the following error is generated: Index was out of range. Must be non-negative
      ' and less than the size of the collection. Parameter name: index

      ' Sort elements
      arrWithDuplicates.Sort()

      ' Copy array with duplicates to another array
      Dim arrNoDuplicates = arrWithDuplicates.Clone()
      intCount = arrWithDuplicates.Count

      For i = (intCount - 1) To 1 Step -1
         If (arrNoDuplicates(i).ToString() = arrNoDuplicates(i - 1).ToString()) Then
            arrNoDuplicates.RemoveAt(i)
         End If
      Next i
ExitPoint:
      Return arrNoDuplicates

   End Function


End Module
 
Solved - Out of range

It worked.
Here's the For loop where the message was being generated:
For i = (intCount - 1) To 1 Step -1
If (arrNoDuplicates(i).ToString() = arrNoDuplicates(i - 1).ToString()) Then
arrNoDuplicates.RemoveAt(i)
End If
Next i

Thanks a bunch.
 
Hi Guys,

I have just posted something similar for another thread so I have posted a similar example here as another example of how you can achieve your uniqueness.

VB.NET:
  Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
    'here we split a string into a list and at the same time we convert to lower case
    'I have used a List(of String) here instead of an array list since you can 
    'strongly type a list whereas as ArrayList is of Anonymous Type which causes additional
    'complications in the LINQ query
    Dim strWords As New List(Of String)
    strWords.AddRange(TextBox1.Text.ToLower.Split(" "c))
 
    'here I use a LINQ statement to group the words for uniqueness, and then select the Group to be returned
    'as a new array
    Dim strUniqueWords() As String = (From Word In strWords Select unqWord = Word Group By unqWord Into G = Group).Select(Function(x) x.unqWord).ToArray
 
    'display the results
    TextBox2.Text = String.Join(" ", strUniqueWords)
  End Sub

Hope that helps.

Cheers,

Ian
 
Back
Top