1

I need to pass more than 10 parameters from one method to another all are of a different type.

If I pass them as parameters it's not looking nice.

thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
user3631120
  • 99
  • 1
  • 1
  • 4
  • 6
    send an object. – user1666620 Aug 14 '15 at 09:50
  • 1
    group together similar parameters in to object (class) and sent it instead – Milen Aug 14 '15 at 09:52
  • 3
    if you have function calls with +10 parameters, then you may need to refactor your code, and create more functions. Be more precise about your current use case and show some code if you want help – Eloims Aug 14 '15 at 09:53
  • 4
    Normally you would encapsulate all the arguments in a class. However, depending on the arguments, you might want to encapsulate them in more than one class, if some subsets of the arguments are highly coupled. Can you give some sample code? There might be much better approaches. – Matthew Watson Aug 14 '15 at 09:54
  • Please, learn to use a [search engine](https://www.google.com/search?q=pass+many+parameters+c%23+site:stackoverflow.com). – Jonathon Reinhart Aug 14 '15 at 09:59

1 Answers1

3

Create a class like the below:

public class ParamObject 
{
    public string Blah1 { get; set; }
    public DateTime Blah2 { get; set; }
}

And instantiate it and use it in your methods.

public void DoStuff (ParamObject uglyRecord)
{
    //do stuff...
}

public void CallStuff()
{
    ParamObject uglyRecord = new ParamObject();
    uglyRecord.Blah1 = "things";
    uglyRecord.Blah2 = DateTime.Now();

    DoStuff(uglyRecord);
}

However, a method that accepts lots of parameters or complex objects is probably a sign that the method is doing too much and should be refactored into several smaller and simpler methods.

user1666620
  • 4,800
  • 18
  • 27