I'm having difficulty using a foreach statement with a WinForm ListView control. The following two code blocks demonstrates what I'm trying to do. It works with a for loop, but not a foreach.
foreach(var item in listView.Items){
item. <-Can't access any of the subitems of this item
}
vs
for(int i=0;i<listView.Items.Count;i++){
listView.Items[i].Subitems[1] <- Here I can access the sub items
}
I'm trying to use a foreach loop so I can more easily remove items from the ListView.
You need to specify the type:
foreach(ListViewItem item in listView.Items){
To answer your comments:
This is because most controls' item collections implement the non-generic ICollection
(and IEnumerable
), see this MSDN entry for ListViewItemCollection
for example. Since it doesn't implement the generic ICollection<T>
or IEnumerable<T>
, the compiler can't guess the type of the items from looking at the collections themselves, so you have to tell it that they're of type ListViewItem
instead of using var
.
You need to specify the type if the item in the collection explicitly. The var
keyword uses type inference in order to determine the type of the variable. In the case of var
in a foreach
clause, it uses the particular implementation of IEnumerable
to determine the type.
IEnumerable
(and not a generic IEnumerable<T>
), then var
will be object
IEnumerable<T>
(say, IEnumerable<int>
), then var
will be T
(in the example here, var
would be int
)In your case, ListViewItemCollection
does not implement any generic form of IEnumerable<T>
, so var
is assumed to be object
. However, the compiler will allow you to specify a more specific type for the iterator variable if the enumerable only implements IEnumerable
, and it automatically inserts a cast to that particular type.
Note that, because there's a casting operator, the cast will fail at runtime if the object is not of that particular type. For instance, I can do this:
List<object> foo = new List<object>();
foo.Add("bar");
foo.Add(1);
foreach(string bar in foo)
{
}
This is legal, but will fail when the iterator reaches the second item, since it is not a string
.
You need to have the type of the item - in this case: ListViewItem
.
Also, if you're planning to remove items from the collection and are using a foreach loop, you cannot directly remove from the you're looping through - you'd need to add each item to remove to a new collection and remove all items in that collection from the original after the termination of the loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With