选择两个不同的变量“集合”。在没有两个循环的循环条件的基础上C#中的listview项目,我该怎么做? [英] Choose two different variable "collections" of listview items in C# on base of a condition for looping over them without two loops, how would I do that?

查看:87
本文介绍了选择两个不同的变量“集合”。在没有两个循环的循环条件的基础上C#中的listview项目,我该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#WinForm中预先选择listView项的变量集合,并为每个项目添加不同的数据类型,然后我进入循环迭代项目而不实际加倍循环本身以应用条件?



  bool  SelectAll =  false ;  //  可以是true或false它是一个变量设置...  
var myItems = null ; // 这里第一个错误,初始化需要var而不能为null,那该怎么办?它必须是一个空类型才能在var中预先选择一个类型,否则var不能再被改变为另一个类型。

/ * 这里应该决定哪个项目集合需要花费b $ b来循环。是的,我只是可以使用所有这些代码放置两个循环,但是这会重复所有这些。我不能做的函数,因为ListViewItemCollection和ListViewSelectedItemCollection的数据类型不相同,并且枚举不允许对象类型(我试过)!* /

if (SelectAll == true
{
myItems = listView1.Items; // 问题:此Listview集合与下面的其他数据类型不同!
}
else
{
myItems = listView1.SelectedItems; // 问题:此Listview集合与上面的其他数据类型不同!
}

foreach (ListViewItem XYZ in myItems) // 这里的循环不会重复通过条件,只有一个循环,但循环的项目选择可能不同
{
// ...中间有许多代码行
}





我尝试过:



尝试'var'数据类型(需要初始化,不能用null初始化,如果我用第一个数据类型初始化我以后不能再添加其他数据类型。它似乎不是可枚举的,不允许返回在函数中)



尝试'对象'数据类型(不能用作en在循环中无法理解)



尝试将'ListViewItemCollection'作为ienumerable,但它不能采用像'ListViewSelectedItemCollection'这样的另一种类型。



所有代码条件重复的第二个循环是我不想要的!

我尝试了一个函数,但返回数据类型因条件而异,它可以是'ListViewItemCollection'或'ListViewSelectedItemCollection'。所以一个函数也不适用。



在循环之前是否有人想要变量项选择?

解决方案

由于 ListViewItemCollection SelectedListViewItemCollection 都实现 IEnumerable ,我建议将 myItems 声明为 IEnumerable

 IEnumerable myItems; 

if (SelectAll)
{
myItems = listView1.Items;
}
else
{
myItems = listView1.SelectedItems;
}

foreach (ListViewItem XYZ in myItems)
{
// ...介于两行之间的多行代码
}


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; //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?

解决方案

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

IEnumerable myItems;

if (SelectAll)
{
  myItems = listView1.Items; 
}
else
{
  myItems = listView1.SelectedItems; 
}
 
foreach (ListViewItem XYZ in myItems)
{
  //...many lines of codes in between
}


这篇关于选择两个不同的变量“集合”。在没有两个循环的循环条件的基础上C#中的listview项目,我该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆