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.