0

I am coding my first game in C#, and I am still pretty new to C#. Coming from javascript, I tend to make big objects and then pass them to a function that then deals with it. Is it a good approach to use the same concept in C#?

Le example

    public struct GameParams {
        public List<Player> players;
        public int gridX = 0;
        public int gridY = 0;
    }

  class Game{
       private Grid grid;
       private List<Players> players;

       public Game(GameParams gameParams) {
            grid = new Grid(gameParams.gridX, gameParams.gridY);
            players = gameParams.players;
            dostuff();
        }
    }

Does this make any sense?

user3822370
  • 641
  • 7
  • 20
  • 1
    One tip: Don't use `struct`. As a new C# programmer, stick to `class`. More info: http://stackoverflow.com/questions/521298/when-to-use-struct – Blorgbeard Jul 15 '15 at 23:32
  • What specific problem are you trying to solve? Any answer to this would be opinionated. With that said, my opinion (ahem) is that three parameters is not that bad. Now if you start having 6 or 7 or 8 or more, then I'd start questioning the design. I don't see any value in creating a struct simply for the purpose of reducing the numbers of parameters a method (in this case a constructor) takes. Rather, I would say that it may be time to take a step back and look at the bigger design picture. The single responsibility principle comes to mind... – James R. Jul 16 '15 at 00:33

1 Answers1

1

Yes you should make classes and pass them to functions. Getting over three parameters you need to take a step back and ask "Should this be a class?"

  • Thanks. I think I realized this early on and I'm glad you confirmed it. I'm a novice programmer and so far I've seen a function that takes over 100 variables. Unreal! – user3822370 Jul 16 '15 at 01:01