user6607088
user6607088

Reputation:

How use return between methods

In my Program I have a button click, from that onclick event I am calling one method for some textbox validation. The code is:

  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
  }

Here if txtphysi.text is null then it comes into loop and use return then it came from the CheckValidation() method only and it continues in btnupdate_Click event, but here I want to stop the execution process in btnupdate_Click also. How can I do it?

Upvotes: 2

Views: 167

Answers (5)

Akshay
Akshay

Reputation: 1472

Here try this:

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

}

Upvotes: 0

Nick Goloborodko
Nick Goloborodko

Reputation: 3053

To achieve this:

1) Change the return type of the CheckValidation() method to bool. In your btnupdate_Click() method do something along the lines of

if(!CheckValidation())
{
    return;
}

Hope that helps.

Upvotes: 4

A.T.
A.T.

Reputation: 26352

Though answer has been already accepted, but I believe this is not good practice to put code in events. Better to use delegates for such purposes.

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();
    }

}

Upvotes: 2

Harsh Baid
Harsh Baid

Reputation: 7249

It is very simple programming logic in my understanding that you need to apply here..

That is return Boolean from CheckValidation() method instead of return void so that parent function knows the state from the function's execution.

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

Upvotes: 9

Svein Terje Gaup
Svein Terje Gaup

Reputation: 1578

If this is ASP.NET, which you have tagged it as, then you should perhapts use a RequiredFieldValidator (for webforms), and you could use DataAnnotations if you are using MVC: http://forums.asp.net/t/1983198.aspx?RequiredFieldValidator+in+MVC

Upvotes: 2

Related Questions