3

I have a function:

internal static void GetUserData(int userId, out string userName,
                                 out string userEmail, out string userPassword)
{

  using (SqlConnection con = Util.GetConnection())
  {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("usp_UD_SelectById", con))
    {
      cmd.CommandType = CommandType.StoredProcedure;

      cmd.Parameters.Add("@UD_ID", SqlDbType.Int).Value = userId;
      cmd.Parameters.Add("@UD_UserName", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Output;
      cmd.Parameters.Add("@UD_Password", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Output;
      cmd.Parameters.Add("@UD_Email", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Output;


      cmd.ExecuteNonQuery();

      userName = Convert.ToString(cmd.Parameters["@UD_UserName"].Value);
      userEmail = Convert.ToString(cmd.Parameters["@UD_Email"].Value);
      userPassword = Convert.ToString(cmd.Parameters["@UD_Password"].Value);

    }
  }
}

and the call

string userEmail;
string userName;
string userPassword;
MemberHelper.GetUserData(userId, out userName, out userEmail, out userPassword);

Sometimes I need to get just one parameter from the out parameters, how can I call the function when I want to get just one:

string userPassword;
MemberHelper.GetUserData(userId,"","",out userPassword);

1 Answer 1

4

You have to supply them. out parameters are not optional. You could write a custom overload that only provides the out parameters that you need.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.