关于collection的操作注意。

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

   写了个程序,用来从一个panel中将我自己写的user control移动到另外一个panel,如下

 

         private void MoveViewers(InfoList sourceList, InfoList targetList)

         {

              foreach (object viewer in sourceList.Controls)

              {

                   if (((InfoViewer) viewer).Checker.Checked)

                        targetList.Controls.Add((InfoViewer) viewer);

              }

         }

   

   谁知道,居然出错,郁闷,想了想,应该是Controls在变造成foreach找不到对象了。

   修改如下:

 

        

private void MoveViewers(InfoList sourceList, InfoList targetList)

         {

              Queue queqe = new Queue();

 

              foreach (object viewer in sourceList.Controls)

              {

                   if (((InfoViewer) viewer).Checker.Checked)

                       queqe.Enqueue(viewer);

              }

 

              while (queqe.Count != 0)

              {

                   targetList.Controls.Add((InfoViewer) queqe.Dequeue());

              }

         }

 

   BUG修复,已经好用了。

 

   感叹,OOP一不小心,一个想当然的想法就会造成一个BUG,太恐怖了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章