回到上一个循环?

时间:2011-05-26 17:51:10

标签: c# for-loop foreach

我将如何返回另一个循环?

 for (int index = MIN_DIAGNOSIS; index <= MAX_DIAGNOSIS; index++) 
   {
       foreach (RepeaterItem ri1 in GeneralRepeater.Items)
       {
           int iItemIndex = ri1.ItemIndex;
           var myDDL = GeneralRepeater.Items[iItemIndex].FindControl("derp");
           MyPoc.Diagnoses.Diagnoses[index] = new PatientDiagnosis(/*snip*/);
           return index; 
           //error 'PatientPlanOfCare.cmdSave_Click(object, System.EventArgs)' 
           //returns void, a return keyword must not be followed by 
           //an object expression
      }
    }
    break;

2 个答案:

答案 0 :(得分:2)

只需打破内部循环并在主循环上使用所需的变量。

for (int index = PocDiagnoses.MIN_DIAGNOSIS; index <= PocDiagnoses.MAX_DIAGNOSIS; index++) 
{
    foreach (RepeaterItem ri1 in GeneralRepeater.Items)
    {
        int iItemIndex = ri1.ItemIndex;
        DropDownList myDDL = (DropDownList)GeneralRepeater.Items[iItemIndex].FindControl("GeneralDDL");
        MyPoc.Diagnoses.Diagnoses[index] = new PatientDiagnosis(myDDL.SelectedValue, new SynergyOnSetDate(new System.DateTime(Year, Month, Day)), "01/02/2011");
        break;
    }

    // Check the index variable here.
}

答案 1 :(得分:-2)

就像评论的那样,很难说出你想要做什么。 根据注释中的错误消息,此代码在返回void的方法中执行,这意味着您的return语句后面不应该有index