C# return easy to make mistakes

Lionsure 2020-08-31 Original by the website

The "return" is used to return in the process of program development, either from a sub-function(method), or to end the current program by returning. Especially in the latter case, you may make mistakes if you don't pay attention. Why do you say that?

 

C# return easy to make mistakes

If you want to implement a function of submitting a product form, several fields are required. In order to ensure safety, in addition to checking with Javascript in the foreground, you must check with C# again in the code behind. During inspection, if the required field is found to be empty, it will return the prompt "Required field cannot be empty". The code is as follows:

//Submit code
       protected void btn_Click(object sender, EventArgs e)
       {
              if (string.IsNullOrEmpty(tbProductName.Text))
              {
                     lblErr.Text = "Product name cannot be empty!";
                     lblErr.Visible = true;
                     return;//Return to end program run
              }
       }

 

Only a "cannot be empty" prompt is written in the code. When you are ready to write the second prompt, you will find that the code implementation is the same as the first prompt. The only difference is that the prompt text is different. The code is as follows:

//Submit code
       protected void btn_Click(object sender, EventArgs e)
       {
              if (string.IsNullOrEmpty(tbProductName.Text))
              {
                     lblErr.Text = "Product name cannot be empty!";
                     lblErr.Visible = true;
                     return;//Return to end program run
              }

       if (string.IsNullOrEmpty(tbPrice.Text))
              {
                     lblErr.Text = "Product price cannot be empty!";
                     lblErr.Visible = true;
                     return;//Return to end program run
              }
       }

If each prompt is written in this way, the code repetition rate is too high, so the prompt code is encapsulated into a method, and you can call it directly where there is no empty prompt. The code is as follows:

//Check if it is empty
       private void CheckEmpty(string val, string message)
       {
              if (string.IsNullOrEmpty(val))
              {
                     lblErr.Text = message;
                     lblErr.Visible = true;
                     return;//Return to end program run
              }
       }

The submission code becomes as follows:

protected void btn_Click(object sender, EventArgs e)
       {
              CheckEmpty(tbProductName.Text, "Product name cannot be empty!");
              CheckEmpty(tbPrice.Text, "Product price cannot be empty!");
       }

I don't know if you found out, but in the process of encapsulating the repeated prompt code into a method logically, an error has occurred. You might ask, the code is still the original code, and there is no change how there is an error.

 

When return is moved from the click event(btn_Click) to the method(CheckEmpty), there is a change, from ending the running of current program to returning from the current method, that is, the code that calls it will continue to run down, which violates the original intention of returning and ending the program running when the required field is empty, so the return must be moved up, and the code becomes:

//Check if it is empty(C# return bool)
       private bool CheckEmpty(string val, string message)
       {
              if (string.IsNullOrEmpty(val))
              {
                     lblErr.Text = message;
                     lblErr.Visible = true;
                     return true;
              }
              else
                     return false;

       }

The submission code becomes as follows:

protected void btn_Click(object sender, EventArgs e)
       {
              if(CheckEmpty(tbProductName.Text, "Product name cannot be empty!")) return;
              if(CheckEmpty(tbPrice.Text, "Product price cannot be empty!")) return;
       }

 

Attachment: Foreground code:

<ul>
              <li>Product name: <asp:TextBox ID="tbProductName" runat="server" /></li>
              <li>Product price: <asp:TextBox ID="tbPrice" runat="server" /></li>
              <li><asp:Button ID="btn" runat="server" OnClick="btn_Click" />
                     <asp:Label ID="lblErr" runat="server" Visible="false" />
              </li>
       </ul>