简体   繁体   中英

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

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

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;

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.

Starting with C# 4.0 you have the ability to pass by name and use default values, but I don't know if that's enough in your case.

There's the option of pivoting your arguments. Instead of passing all those parameters you might wanna have an arguments object which contains these properties. That might have more of an object oriented feel to it.

Lastly, there's the dynamic approach. That's passing a dictionary of string and value pairs. This is a slightly different approach as you lose type safety but it does have it uses as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM