Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How would I pre-select a variable collection of listView items in C# WinForm with a different datatype for each, before I enter a loop with iterating through the items without doubling actually the loop itself for applying the condition?

C#
bool SelectAll=false; //can be true or false it is a variable setting...
var myItems=null; //here first error, initialization is required of "var" and cannot be "null", so what to do? it must be an empty type to not preselect a type in var, else var cannot be changed anymore to another type.

/*here shall be the decision which collection of items shall 
be taken to loop through. Yes, I simply could put two loops with all this code, but that would suck to repeat all this. A function I cannot do, because the data type of the ListViewItemCollection and ListViewSelectedItemCollection is not the same and an object type is not allowed for enumeration (I tried it)!*/

if(SelectAll==true)
{
myItems=listView1.Items; //problem: this Listview Collection is a different data type as the other below!
}
else
{
myItems=listView1.SelectedItems; //problem: this Listview Collection is a different data type as the other above!
}

foreach(ListViewItem XYZ in myItems) //here shall be the loop not repeat through the condition, just one loop but the selection of the items to loop through could be different
{
//...many lines of codes inbetween
}


What I have tried:

tried 'var' datatype (needs to be initialized, cannot be initialized with null, if I initialize with the first data type I cannot later put another data type on it. it seems not to be enumerable and is not allowed to be returned in a function)

tried 'object' datatype (cannot be used as enumerable in the loop)

tried 'ListViewItemCollection' as ienumerable, but it cannot take another type like 'ListViewSelectedItemCollection'.

A second loop with all the code conditional repeated is what I do not want!
I tried a function, but the return datatype varies on a condition, it can be either 'ListViewItemCollection' or 'ListViewSelectedItemCollection'. So a function doesn't apply either.

Has someone another idea for variable items selection before the loop?
Posted
Updated 29-Apr-16 14:20pm
v4

1 solution

Since ListViewItemCollection and SelectedListViewItemCollection both implement IEnumerable, what I'd suggest is to declare myItems as IEnumerable:
C#
IEnumerable myItems;

if (SelectAll)
{
  myItems = listView1.Items; 
}
else
{
  myItems = listView1.SelectedItems; 
}
 
foreach (ListViewItem XYZ in myItems)
{
  //...many lines of codes in between
}
 
Share this answer
 
Comments
Karthik_Mahalingam 29-Apr-16 22:20pm    
5
Matt T Heffron 1-May-16 2:07am    
Add:
using System.Collections;
to the file.
If you had right clicked on the IEnumerable with the wiggly underline, one of the options would be "Resolve >" and in the slide out menu is that "using" as above. Click that and Visual Studio would have added it to the file for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900