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?
bool SelectAll=false;
var myItems=null;
if(SelectAll==true)
{
myItems=listView1.Items;
}
else
{
myItems=listView1.SelectedItems;
}
foreach(ListViewItem XYZ in myItems)
{
}
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?