Since my project has fairly become more complex,
I aslo thought maybe other things are messing it up.
So I created a new Project (Windows Form) with a ListView (lstvw_test)
Then I added the following code to the form_load event:
Visual Basic:
Dim LstObjArray As New System.Collections.ArrayList
Dim I As Int16
For I = 0 To 100
Dim LstObj As New ListViewItem
LstObj.Text = "a" & Str(I)
LstObj.SubItems.Add("b" & Str(I))
LstObjArray.Add(LstObj)
Next
lstvw_test.Items.AddRange(LstObjArray.ToArray)
The wierd part is when I add the line Debug.print(LstObjArray.Item(0).text),
the correct text is printed (not the error as it should be when the Editor was correct).
Although it may seem as it was just an error on the editors behalf,
as soon as the AddRange Sub is called, an exception is thrown (System.InvalidCastException).
DirectCast(LstObjArray.ToArray, ListViewItem()) also throws the exception.
I attached the project of the scenario described above.
The following text is not necessary to understand the problem better but it might give some ideas on alternative solutions.
Background on what I'm trying to achieve:
Files/Folders (e.g. Exlorer) are dragged to the Form,
then the filenames should be added to the ListView.
The only condition: No double entries.
So at first I just made a Loop with the String array,
comparing all the items in the ListView(.IndexOfKey([String])) with the "ToBeAdded" String,
If its not found, add it and if it is don't.
But as I'm now painfully aware, this is extremly slow.
After thinking about it some time, I had an idea which could have saved the day,
but no, now I have this problem as described above.
The idea was, that the dropped files don't need to be checked against each other
since it is impossible(?) that there is a duplicate and
therefore only need to be compared against the initial Items in the ListView.
In order to do that I need to Buffer the ListViewItems (LstObjArray),
before adding them to the ListView.