Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#-如何将多个参数传递给一个方法?_C#_Asp.net - Fatal编程技术网

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

c#-如何将多个参数传递给一个方法?,c#,asp.net,C#,Asp.net,我有一个方法需要9个参数(3个是int,6个是string)。处理这个问题的最好方法是什么?我应该如何在不指定这么多参数的情况下调用该方法 方法看起来像这样 public void addProfileData(string profileText, string emailText, string serverText, int repeatText, int timeoutText, string urlText, string elementIDText, string textToBeV

我有一个方法需要9个参数(3个是int,6个是string)。处理这个问题的最好方法是什么?我应该如何在不指定这么多参数的情况下调用该方法

方法看起来像这样

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);
    }
}
paramprofileText.Value = model.ProfileText;

创建一个包含用户配置文件数据的配置文件对象,并将其传递给程序。您可能在很多地方同时使用这些信息,它们是应用程序域模型的一部分

伊文德·克诺布洛赫·布莱恩的回答给出了一个例子。您应该更仔细地考虑应用程序中的哪些数据位在逻辑上相互关联。例如,您是否希望将一个用户的姓名和另一个用户的电子邮件传递到此功能?如果不是这样,那么这就是使用类保存逻辑上共存的数据的另一个好例子。如果你想允许<代码>用户配置文件类是可变的(有属性设置器),或者允许应用程序的一部分创建实例但不修改它们。

< P>从C 4开始,你有能力通过名字并使用默认值,但是我不知道在你的情况下这是否足够了。 还有一个选择,就是旋转你的论点。您可能不希望传递所有这些参数,而是希望有一个包含这些属性的arguments对象。这可能更像是一种面向对象的感觉


最后,还有动态方法。这是传递字符串和值对的字典。这是一种稍有不同的方法,因为您失去了类型安全性,但它也有自己的用途。

要使其看起来更整洁,请将参数设置为它自己的类

因此,创建这样一个类:

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 )
然后您可以像这样访问方法内部的所有参数

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);
    }
}
paramprofileText.Value = model.ProfileText;

嗯。。。。调用该方法时包括所有9个参数?创建一个逻辑上包含所需数据的属性的类,然后将该类的有效实例作为参数传递?除了围绕特定问题的建议之外,你应该开始使用
语句,而不是显式地调用
关闭
——否则,如果出现异常,你可能会泄漏资源。在发布一些示例代码(一件好事)时,尝试减少到那些部分,这是真正需要演示你的问题的部分!它不会解决这个问题,但您可能还对params关键字感兴趣。查找它以供将来参考,它对类似场景非常有用。谢谢。你有什么例子吗?