Sunday, August 25, 2013

c# List, class instances

how can we create 9 new instances and then passing it....

Passing what and where? How are you getting the user input? I'll make a few assumptions and suggest accordingly:



// create the list with 9 cats
List<Cat> cats = new List<Cat>();
for (int i=0; i < 9; i++)
{
cats.Add(new Cat());
}

// input from users
// Don't know how you want to do this part
// Assuming Console.ReadLine() in a loop
// for number of cats already specified (9 in your example)

int Age;
string Name;
foreach (Cat cat from Cats)
{
Console.WriteLine("Age of Cat: ");
int.TryParse(Console.ReadLine(), out Age);
cat.Age = Age;
Console.WriteLine("Name of Cat: ");
cat.Name = Console.ReadLine();
}





~~Bonnie Berent DeWitt [C# MVP]


http://geek-goddess-bonnie.blogspot.com


No comments:

Post a Comment