简体   繁体   English

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

[英]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). C# - 我有一个需要9个参数的方法(3个是int,6个是字符串)。 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. ØyvindKnobloch-Bråthen的回答显示了一个例子。 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. 您可能还想考虑是否要允许UserProfile类是可变的(具有属性设置器)或允许应用程序的某些部分创建实例但不修改它们。

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. 从C#4.0开始,您可以通过名称传递并使用默认值,但我不知道在您的情况下这是否足够。

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. 这是一种稍微不同的方法,因为您失去了类型安全性,但确实也使用了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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