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:
Then, change the method signature to:
Then you can access all parameters inside the method like this