Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# - How to pass multiple arguments to a method?

Tags:

c#

asp.net

C# - I have a method which requires 9 arguments (3 are int and 6 are string). What is the best way to deal with this? How should i call the method without specifying so many arguments.

Method looks something like this

public void addProfileData(string profileText, string emailText, string serverText, int repeatText, int timeoutText, string urlText, string elementIDText, string textToBeVerifiedText, int benchmarkTime)
{
    int pid = -1;
    SqlCommand myCommand = new SqlCommand("AddProfileInformation", myConnection);
    myCommand.CommandType = CommandType.StoredProcedure;
    SqlParameter paramprofileText = new SqlParameter("@profileText", SqlDbType.NVarChar);
    paramprofileText.Value = profileText;
    myCommand.Parameters.Add(paramprofileText);

    SqlParameter paramemailText = new SqlParameter("@emailText", SqlDbType.NVarChar);
    paramemailText.Value = emailText;
    myCommand.Parameters.Add(paramemailText);

    myConnection.Open();
    using (SqlDataReader rdr = myCommand.ExecuteReader(CommandBehavior.CloseConnection))
    {
        while (rdr.Read())
        {
            pid = rdr.GetInt32(rdr.GetOrdinal("pid"));
        }
        rdr.Close();
    }
    myConnection.Close();
    if (pid != -1)
    {
        addPingData(serverText, repeatText, timeoutText, pid);
        addPLTData(urlText, elementIDText, textToBeVerifiedText, benchmarkTime, pid);
    }
}
like image 655
Kartik Avatar asked Apr 11 '11 06:04

Kartik


2 Answers

To make this look tidier, make the parameters into a class of it's own.

So make a class like this:

public class ProfileViewModel{
  public string ProfileText{get;set;};
  public string EmailText{get;set;};
  public string ServerText{get;set;};
  public int RepeatText{get;set;};
}

Then, change the method signature to:

public void addProfileData( ProfileViewModel model )

Then you can access all parameters inside the method like this

paramprofileText.Value = model.ProfileText;
like image 159
Øyvind Bråthen Avatar answered Oct 02 '22 14:10

Øyvind Bråthen


Create a profile object for containing the user profile data and pass that around your program instead. You're probably using these bits of information together in lots of places, they're part of your application's domain model.

Øyvind Knobloch-Bråthen's answer shows an example. You should think a bit harder which bits of data in your application are logically associated with each other. For example would you ever want to pass one user's name and another user's email to this function? If not then that's another good case for using an class to hold data that logically lives together. You might also want to consider if you want to allow the UserProfile class to be mutable (have property setters) or allow parts of your application to create instances but not modify them.

like image 26
Ade Miller Avatar answered Oct 02 '22 15:10

Ade Miller