如何在c#中停止执行条件(if-loop) [英] How to stop execution of condition(if-loop) in c#

查看:712
本文介绍了如何在c#中停止执行条件(if-loop)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想停止if循环的执行,我已尝试使用'return'语句但它退出函数,那么如何退出单个if Statement.I尝试使用以下代码...



Hi I want to stop the execution of if-loop ,I have tried with 'return' statement but its exits from the function ,So how can I exit from the single if Statement.I have tried with following code...

void A()
{

          if (CheckHorizontalSide(SourceMember))
            {
                
                if (lblHorizontalMember.Text == DestinationMember)
                {
                    
                    lsRelationPath.Add(lblHorizontalMember.Text);
                    lblRelationPath.Text = String.Join("-", lsRelationPath);
                    lblRelationPath.Visible = true;
                    return;
                }

                bool WhetherContains = lsRelationPath.Contains(SourceMember);
                if (WhetherContains)
                {
                    return;
                }

                lsMemberID1.Clear();
                lsRelationPath.Add(lblHorizontalMember.Text);
               

                Find_Route(lblHorizontalMember.Text, DestinationMember);
            }


	    if(CheckTop(SourceMember))
	    {
		//code here....
	    }
}

推荐答案

只需要重新组织逻辑:

Just need to re-organise the logic:
if (CheckHorizontalSide(SourceMember))
  {
      bool WhetherContains = lsRelationPath.Contains(SourceMember);

      if (lblHorizontalMember.Text == DestinationMember)
      {

          lsRelationPath.Add(lblHorizontalMember.Text);
          lblRelationPath.Text = String.Join("-", lsRelationPath);
          lblRelationPath.Visible = true;
      }
      else if (!WhetherContains)
      {
           lsMemberID1.Clear();
           lsRelationPath.Add(lblHorizontalMember.Text);
          Find_Route(lblHorizontalMember.Text, DestinationMember);
      }

  }


这篇关于如何在c#中停止执行条件(if-loop)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆