3

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.

Ankit Kumar
  • 397
  • 2
  • 11
  • 21
  • 2
    This is a little beyond where you are at now. As a small preview of whats to come however: arrays are not the type you want to use when you are dealing with an unknown size. The solution would be to implement a collection and then add to that collection every time you want to add another animal. – maccettura Nov 21 '17 at 20:59
  • 1
    You can use a `List` instead of an `Animal[]`. Then you can call `thatList.Add(new Animal( .. ));` – Blorgbeard Nov 21 '17 at 21:00
  • Use `List` rather than `Animal[]`. – Enigmativity Nov 21 '17 at 21:00
  • 1
    You also want to declare/create that list *above* the loop, not inside it. – Blorgbeard Nov 21 '17 at 21:01

2 Answers2

4

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();
}
trashr0x
  • 6,457
  • 2
  • 29
  • 39
  • Thanks for the help I didn't know about lists! Can they be utilized like an array otherwise? –  Nov 22 '17 at 00:51
  • @JGoss yes they can - as for use cases of one over the other, have a look at [this answer](https://stackoverflow.com/a/434765/4302070). – trashr0x Nov 22 '17 at 01:51
0

Arrays are fixed in size once declared. The easiest way to have a dynamic collection size is to use List class instead. If you're restricted to using arrays, you could start with array of fixed size (say 1). Then, when you need more space, you would allocate a new array twice as large as original, copy original array into the new one and then add new data. You can repeat this process as needed.

DanielS
  • 744
  • 6
  • 13
  • 3
    Which is in fact what `List` does for you. – Eric Lippert Nov 21 '17 at 21:07
  • @DanielS `List`s start with a default capacity which keeps doubling when expansion is needed. – trashr0x Nov 21 '17 at 21:14
  • 1
    I think all of us agree that List is better suited for this task. But, since the question is about arrays, I showed how it can be done. And yes, that's how List does it too. – DanielS Nov 21 '17 at 21:16
  • I did not know about lists if they work better I will most certainly use them. –  Nov 22 '17 at 00:41