不能从foreach循环中走出来

时间:2017-03-23 08:43:02

标签: c# coded-ui-tests

我不知道为什么在获取属性和break语句后它无法从循环中退出。

int i = 2;

foreach (UITestControl con in uIItemCustom.GetChildren())
                {

                    Description = new HtmlCell(con);
                    Description.FilterProperties[HtmlCell.PropertyNames.ColumnIndex] = "4";
                    Date = new HtmlCell(con);
                    Date.FilterProperties[HtmlCell.PropertyNames.ColumnIndex] = "5";

                if (TestContext.DataRow["BillableLine"].ToString() == Description.InnerText.ToString() && TestContext.DataRow["Date"].ToString() == Date.InnerText.ToString())
                        {
                        HtmlSpan chk = FindControl<HtmlSpan>(x =>
                        {
                            x.Add(HtmlSpan.PropertyNames.Id, "timeMaterialGrid0_ctl0" + i + "_TriStateChkB");
                            x.Add(HtmlSpan.PropertyNames.TagName, "SPAN");
                        });
                        Mouse.Click(chk);
                        break;
                        }              
                    i++;         
            }

1 个答案:

答案 0 :(得分:-1)

请参阅下面的答案,同时check this documentation要记住C#break语句。

int i = 2;

foreach (UITestControl con in uIItemCustom.GetChildren())
                {

                    Description = new HtmlCell(con);
                    Description.FilterProperties[HtmlCell.PropertyNames.ColumnIndex] = "4";
                    Date = new HtmlCell(con);
                    Date.FilterProperties[HtmlCell.PropertyNames.ColumnIndex] = "5";

                if (TestContext.DataRow["BillableLine"].ToString() == Description.InnerText.ToString() && TestContext.DataRow["Date"].ToString() == Date.InnerText.ToString())
                        {
                        HtmlSpan chk = FindControl<HtmlSpan>(x =>
                        {
                            x.Add(HtmlSpan.PropertyNames.Id, "timeMaterialGrid0_ctl0" + i + "_TriStateChkB");
                            x.Add(HtmlSpan.PropertyNames.TagName, "SPAN");
                        });
                        Mouse.Click(chk);
                        break;
                        }
            // This is the part you need to add - an else and break
else{
  break;
}              
                    i++;         
            }