gpt4 book ai didi

c# - 如何将多个参数传递给一个方法?

转载 作者:太空狗 更新时间:2023-10-29 22:17:20 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com