2011-05-26 74 views
0

我將如何返回另一個循環?返回上一個循環?

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; 
+0

你的代碼沒有任何意義。您想做什麼? – 2011-05-26 17:51:55

+0

這很不清楚你在追求什麼。請閱讀http://tinyurl.com/so-hints並相應地編輯你的問題。 – 2011-05-26 17:52:14

+1

'break;'內部循環中的語句會導致它發生並返回到外部循環迭代。 – 2011-05-26 17:54:12

回答

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

沒有太多的觀點使用內部循環,如果你在第一次迭代之後沒有條件地打破... – RedFilter 2011-05-26 17:57:34

+0

如果這真的是OP所需要的,那麼比'foreach'將會毫無用處...... – 2011-05-26 17:57:52

+0

是的,沒有意義。但是,如果這是OP想要的...... – 2011-05-26 17:58:42

-2

就像所評論的那樣,它很難告訴你正在嘗試做什麼。 根據註釋中的錯誤消息,此代碼在返回void的方法中執行,這意味着您的return聲明後面不應有index