Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an array of objects without knowing specified amount ahead of time

I am in an entry level programming class. We have been going over objects lately and were asked to make a program that gets a bit of user input and then creates objects of animals with the attributes we give them. We are only required to make 2 objects but I wanted to spin off on my own and create a program that asks:

For a bit of input, then it puts that info into an undeclared array of objects called animal of class Animal. Then it asks if you'd like to make another animal and if so it repeats the input and putting it into the next element in the array.

I'm having trouble getting it to run, I'm pretty sure I am not initializing the array correctly, I've looked all over stack overflow but I can't find anything that lets me create an array of objects of an unspecified size. I want to create a new object with its constructor values into an element of an array of unspecified size.

Here are the 2 errors I am currently getting:

Error CS0650 Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type

Error CS0270 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)

Here is my main code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayOfObjectsAnimalMaker
{
    class Program
    {
        static void Main(string[] args)
        {
            string another = "y";
            int count = 0;
            string species;
            string age;

            while (another == "y")
            {
                Console.WriteLine("Type in the animal's species: ");
                species = Console.ReadLine();

                Console.WriteLine("Type in the animal's age: ");
                age = Console.ReadLine();

                Animal animal[count] = new Animal(species, age);

                Console.WriteLine("Type y to create another animal or n to end: ");
                another = Console.ReadLine();

                count++;
            }
        }
    }
}

and here is my Animal class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayOfObjectsAnimalMaker
{
    class Animal
    {
        private string species;
        private string age;

        public Animal(string s, string a)
        {
            this.species = s;
            this.age = a;
        }

        public void DisplayInfo()
        {
            Console.WriteLine("This is a " + this.species + " that is " + this.age + " years old.");
        }
    }
}

I am looking forward to learning how to create arrays of objects of an undetermined size.


1 Answers

You can use a List<T>.

List<T> implements IList<T> by using an array whose size is dynamically increased as required.

// create a list to hold animals in 
var allAnimals = new List<Animal>();    

while (another == "y")
{
    Console.WriteLine("Type in the animal's species: ");
    species = Console.ReadLine();

    Console.WriteLine("Type in the animal's age: ");
    age = Console.ReadLine();

    // create the animal..
    var newAnimal = new Animal(species, age);
    // ..and add it in the list.
    allAnimals.Add(newAnimal);

    Console.WriteLine("Type y to create another animal or n to end: ");       
    another = Console.ReadLine();
}
like image 102
trashr0x Avatar answered Jan 31 '26 01:01

trashr0x