作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
C# - 我有一个方法需要 9 个参数(3 个是整数,6 个是字符串)。处理这个问题的最佳方法是什么?我应该如何在不指定这么多参数的情况下调用该方法。
方法看起来像这样
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);
}
}
最佳答案
为了使它看起来更整洁,将参数放入它自己的类中。
所以创建一个这样的类:
public class ProfileViewModel{
public string ProfileText{get;set;};
public string EmailText{get;set;};
public string ServerText{get;set;};
public int RepeatText{get;set;};
}
然后,将方法签名更改为:
public void addProfileData( ProfileViewModel model )
然后你可以像这样访问方法内部的所有参数
paramprofileText.Value = model.ProfileText;
关于c# - 如何将多个参数传递给一个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5617401/
我是一名优秀的程序员,十分优秀!