Still have a problem with 20+ parameters in a class constructor

Sorry if I sound like a broken record guys, but I had a previous thread about this problem and I still haven't figured out a good way to get around it. People have said:

  • Consolidate related variables into classes or structs to lower the number of parameters
  • Serialize to JSON files
  • Object initializers
  • Settable properties
  • Among others

None of these can skirt around the fact that not only do I have a huge collection of this object that takes 20+ parameters (over 100 instances), but all of the parameters are REQUIRED for said class to run correctly AND all present parameters needed will more often than not be unique to that one instance. I actually originally had more parameters, but I found a way to consolidate some into methods due to "common trends" I saw. I still have over twenty.

Maybe my design is fundamentally flawed, but the only other option I KNOW will work is to make individual classes for each character and unit in my game, and add them all in the Dictionary in this class

 public static class CharacterBaseLibrary { private static CharList<string, CharacterCard> charList = new CharList<string, CharacterCard> (); static HeroCard tws; static HeroCard pip; static HeroCard rad; static HeroCard rar; static CharacterBaseLibrary () { tws = new HeroCard (); charList.Add (tws.Name, tws); pip = new HeroCard (); charList.Add (pip.Name, pip); rad = new HeroCard (); charList.Add (rad.Name, rad); rar = new HeroCard (); charList.Add (rar.Name, rar); } public static HeroCard FindHero(string wantedHero) { if (charList.HeroList().ContainsKey (wantedHero)) { return charList.HeroList() [wantedHero]; } else { throw new CharacterNotFoundException(); } } } 

Maybe I just have a broken design, but people accused the CharacterCard class of doing too many things when literally all it does is hold values from the parameters from the constructor and has corresponding properties. Is there a way around this without creating a crapload of classes?

Sorry, I'm just really overwhelmed at all of this. :/

by TheSnowballofCobalt via /r/csharp

Leave a Reply