【问题标题】:How use return between methods如何在方法之间使用 return
【发布时间】:2023-04-06 07:48:02
【问题描述】:

在我的程序中,我有一个按钮单击,从那个 onclick 事件中,我正在调用一种方法来进行某些文本框验证。代码是:

  protected void btnupdate_Click(object sender, EventArgs e)
  {
     CheckValidation();
    //Some other Code
  }

  public void CheckValidation()
  {
    if (txtphysi.Text.Trim() == "")
    {
       lblerrmsg.Visible = true;
       lblerrmsg.Text = "Please Enter Physician Name";
       return;
    }
    //Some other Code
  }

这里如果txtphysi.text 为空那么它进入循环并使用return 然后它仅来自CheckValidation() 方法并在btnupdate_Click 事件中继续,但这里我想在btnupdate_Click 中停止执行过程还。我该怎么做?

【问题讨论】:

  • 认为 "" 是 String.Empty 并且与 null 不同

标签: c# asp.net return


【解决方案1】:

在我的理解中是非常简单的编程逻辑,你需要在这里申请..

即从 CheckValidation() 方法返回 Boolean 而不是 return void 以便父函数从函数的执行中知道状态。

protected void btnupdate_Click(object sender, EventArgs e)
{
    var flag = CheckValidation();
    if(!flag)
        return;
    //Some other Code
}

public bool CheckValidation()
{
    var flag = false; // by default flag is false
    if (string.IsNullOrWhiteSpace(txtphysi.Text)) // Use string.IsNullOrWhiteSpace() method instead of Trim() == "" to comply with framework rules
    {
       lblerrmsg.Visible = true;
       lblerrmsg.Text = "Please Enter Physician Name";
       return flag;
    }
    //Some other Code
    flag = true; // change the flag to true to continue execution
    return flag;
}

【讨论】:

    【解决方案2】:

    要实现这一点:

    1) 将CheckValidation() 方法的返回类型更改为bool。在您的 btnupdate_Click() 方法中,按照

    if(!CheckValidation())
    {
        return;
    }
    

    希望对您有所帮助。

    【讨论】:

    • 另外的方法是什么?
    • 另一种方法是在验证方法中抛出异常并在 Click 方法中捕获它。但这将是一种丑陋的做事方式。
    【解决方案3】:

    如果这是 ASP.NET,您已将其标记为,那么您可能应该使用 RequiredFieldValidator(用于 Web 表单),如果您使用 MVC,则可以使用 DataAnnotations:http://forums.asp.net/t/1983198.aspx?RequiredFieldValidator+in+MVC

    【讨论】:

      【解决方案4】:

      虽然答案已被接受,但我认为将代码放入事件中并不是一个好习惯。最好将委托用于此类目的。

      public class Sample
      {
          public Sample()
          {
              UpdateAction = OnUpdate;
          }
      
          Action UpdateAction = null;
      
          private void OnUpdate()
          {
              //some update related stuff
          }
      
      
          protected void btnupdate_Click(object sender, EventArgs e)
          {
              CheckValidation(UpdateAction);
          }
      
          public void CheckValidation(Action action)
          {
              if (txtphysi.Text.Trim() == "")
              {
                  lblerrmsg.Visible = true;
                  lblerrmsg.Text = "Please Enter Physician Name";
                  return;
              }
              action();
          }
      
      }
      

      【讨论】:

        【解决方案5】:

        这里试试这个:

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            if(!CheckValidation())
            {
                //Some other Code
            }
        }
        
        public bool CheckValidation()
        {
            if (string.isnullorEmpty(txtphysi.Text.Trim()) )
            {
                lblerrmsg.Visible = true;
                lblerrmsg.Text = "Please Enter Physician Name";
                return false;
            }
            else
            {
                //Some other Code
                return true;
            }
        
        }
        

        【讨论】:

        • 将其他代码放在return 语句之后显然是行不通的。
        • else { return true; } //Some other Code 表示您的代码无法访问..:P
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-09
        • 1970-01-01
        • 2017-04-19
        • 2013-08-05
        相关资源
        最近更新 更多