【问题标题】:Return to previous Loop?返回上一个循环?
【发布时间】:2011-05-26 17:51:09
【问题描述】:

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

 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;

【问题讨论】:

  • 您的代码没有任何意义。你想做什么?
  • 目前还不清楚你在追求什么。请阅读tinyurl.com/so-hints 并相应地编辑您的问题。
  • break; 在内循环中的语句会导致它中断并返回到外循环迭代。
  • 是的,你的代码没有意义。您应该创建一个非常简单的示例来说明您的问题并将其粘贴,而不是您的实际代码。此外,如果它适合您的问题而没有滚动条,您更有可能获得帮助。
  • 你为什么不根据你希望你的代码实现什么来重新表述你的问题,而不是问实现问题。这里似乎有些混乱。

标签: c# for-loop foreach


【解决方案1】:

只需打破内部循环并在主循环中使用您想要的变量。

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.
}

【讨论】:

  • 如果您在第一次迭代后中断而没有条件...
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 2020-07-16
    相关资源
    最近更新 更多