Removing elements from an array...

Reapz

Regular
Joined
Dec 15, 2002
Messages
74
I'm having trouble popping elements out of a IO.FileSystemInfo array. I can't use the Remove method so I have to use the Clear method but I think I'm messing it up somewhere.

Here is the section of code...

Visual Basic:
Dim artistdir As New IO.DirectoryInfo(musicroot & "\" & dir.Name)
Dim tracksArray As IO.FileSystemInfo() = artistdir.GetFiles
Dim track As IO.FileInfo
For Each track In tracksArray
       If track.Name = "Thumbs.db" Then
              Dim trackindex = tracksArray.IndexOf(tracksArray, track.Name)
              tracksArray.Clear(tracksArray, trackindex, 1)
       ElseIf track.Name = "Desktop.ini" Then
              Dim trackindex = tracksArray.IndexOf(tracksArray, track.Name)
              tracksArray.Clear(tracksArray, trackindex, 1)
       End If
Next
If tracksArray.Length <> 0 Then.......

As you can see from the last line the subsequent code is dependent on these two files being removed from the array as they don't need to be considerd but even though the values match up the IndexOf method always returns -1. I've tried changing types and using the actual string instead of track.name in the IndexOf statement.

I've just switched from VB6 and I'm still getting me head round .NET so any help would be great.

Cheers!
 
Last edited by a moderator:
You can't just remove items from an array. The clear method you are using is in fact a static method of the Array class and isn't appropriate. You need a collection-based class to be able to remove items like that.
 
Back
Top