Search code examples
c#classloopsobjectinstance-variables

C# How do I allow a user to enter instance variables and create multiple objects?


I am trying to create a program that asks the user "how many player's details would you like to enter?" after they enter this the user is then asked to enter in each of the attributes of these players.

Essentially, I am trying to get the user to instantiate multiple objects of a class.

This is what I have entered into the FootBallPlayer class

class FootballPlayer
{
  private string fullName;
  private int yearBorn;
  private double goalsPerGame;

  // constructor
  public FootballPlayer (string name, int year, double goals)

  {
     fullName = name;
     yearBorn = year;
     goalsPerGame = goals;
  }

  // read-only properties
  public string Name;
  {
     get
       { 
          return fullName;

   public string YearBorn;
  {
     get
       { 
          return yearBorn;
       }
  }

  public string Goals;
  {
     get
       { 
          return goalsPerGame;
       }
  }

In my second class FootballPlayerApp I am trying to get the user to enter firstly the number of players and secondly the details of all of those users.

I have created the following methods GetInput() //which allows the user to enter the number of players and returns it GetName() //which allows the user to enter a players name GetYear() //which allows the user to enter the year born GetGoals() // which allows the user to enter the number of goals scored.

I understand that I can create a single object in the main method as follows

FootballPlayer player1 = new
FootballPlayer ("Lionel Messi", 1988, 2.3);

What I don't understand is

  • how do I have the name of the object (e.g player1 in the example above) be different for each player the user enters when the user is able to enter any number of players.

  • How do I loop it so that multiple players are entered until the numberOfPlayers is reached

  • How do I display the results as follows.

e.g if the user entered 2 players player1 ("Lionel Messi", 1998, 2.3) player2 ("Ronaldo", 1985, 1.4)

how do I get the results to be displayed as

 Player Name    Year Born     Average Goals Scored

 Lionel Messi        1998                      2.3
 Ronaldo             1985                      1.4

Solution

  • how do I have the name of the object (e.g player1 in the example above) be different for each player

    You don't. Instead of individual variables, consider a collection of objects. Basically, any time you find yourself trying to create object1, object2, object3, and so on then you probably want to use an array or collection of some kind instead.

    This could be one of any number of collection types, even a simple array. A common type to use in this case would be List<T>, which for your type would be List<FootballPlayer>.

    Structurally it might look something like this:

    var players = new List<FootballPlayer>();
    var numberOfPlayers = GetInput();
    for (var i = 0; i < numberOfPlayers; i++)
    {
        // prompt the user to enter the next player
    
        var player = new FootballPlayer();
        player.Name = GetName();
        player.Goals = GetGoals();
        // etc.  Basically build up a FootballPlayer object from user input
    
        players.Add(player);
    }
    

    The user would be prompted for each player, and you can use the i variable within the loop to provide useful messages. For example, "Enter the details for player 2:" or something like that.

    After the loop is finished, you'd have a collection of FootballPlayer objects in the players variable.