返回上一个循环?

问题描述

| 我如何在另一个循环上返回?
 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;
    

解决方法

只需中断内部循环并在主循环上使用所需的变量即可。
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.
}
    ,就像评论一样,很难说出您要做什么。 根据您注释中的错误消息,此代码正在以返回void的方法执行,这意味着您的
return
语句后不应有
index
。