-2

I have a very basic questions and I feel like something fundamental is missing because none of my searches seems to answer my question.

What i want to achieve is to create a class lets say Dog with a couple of variablemembers, public int age, public string name, public string breed etc and make a classobject of said class which I would name MyDog and ask the user to give MyDog some values.

Lets say have a class with an array lets call it dogcage and i want to fill it with these class objects myDog from the user.

I thought it would be something like this, pardon my formating I only have a phone available.(code removed since the page didnt accept the formating)

Dogcage[]mydogcage = {New mydog(5, "MrDog", "German shepherd")}

I think im way off on my array where is would need some guidance. I also dont quite grasp how to name an array after something in a different class. I would usually just read more but I have read so much and feel a bit blocked on my logic where i cant reason properly any more.

  • **[Arrays (C# Programming Guide)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/)** even better since it isnt 1998: **[List Class](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netframework-4.8)** – Ňɏssa Pøngjǣrdenlarp Jul 13 '19 at 04:08

1 Answers1

0

If you don't need to store any additional info about the "DogCage" itself, you can just use that as the name of the array (or as the commenter mentioned, a List) like this:

Dog[] dogCage = new Dog[]{ new Dog(5, "MrDog", "German shepherd")};

If you want info about the dog cage, say, a boolean with if it is "clean" or not, you can do this

public class DogCage {
    public bool IsClean {get; set;}
    public List<Dog> Dogs { get; }
    public DogCage(){
        Dogs = new List<Dog>();
    }
}

Then create one and add dogs like so:

DogCage myCage = new DogCage();
myCage.Dogs.Add(new Dog(5, "MrDog", "German shepherd"));
Matti Price
  • 3,351
  • 15
  • 28