Question Get get a type in given context

bondra

Well-known member
Joined
Oct 24, 2020
Messages
77
Programming Experience
Beginner
This driving me nuts. I can't get reach of Toy and really don't know why...
This is what the code looks like stripped down:

C#:
abstract class Animal
{
    public List<Toy> Toy { get; set; } = new List<Toy>();
}

C#:
abstract class Toy
{

}

C#:
class Petowner
{
    int i = 1;
    foreach (var toy in Toy) // 'Toy' is a type, which is not valid in the given context
    {
        if (Toy.OfType<Ball>().Any()) // 'Toy' does not contain a definition for 'OfType'
        {
            if (toy is Ball ball)
            {
                Console.WriteLine(" {0} {1}", i, toy);
                i++;
            }
        }
    }
}
 
@bondra, what book or tutorial are you using to learn about classes and inheritance? It seems like you are missing a lot of the fundamentals.

Anyway, since a PetOwner is presumably not a Animal, then Toy.Animal is not accessible to the PetOwner. Now on the other hand, if you had an instance of an object that is an Animal then you can access that. So let say the PetOwner has a member Animal pet, then you can accessing: pet.Toy to get he list of toys that belong to that Animal.
 
Sources are mixed to be honest. But I forgot the Ball class which is inheriting from the Toy class.
C#:
    class Ball : Toy
    {
    ...
    }

However maybe the class diagram can shed some lights over my way of thinking using class inheritance? ListToys should probably go in the Animal class instead.
ClassDiagram1.png
 
Last edited:
Doh, also noticed I forgot to post the actual method that surrounds the foreach.
As seen I currently has the ListToys method as static. Which gives: "An object reference is required for the non-static field, method or property". But since the Toy class is abstract I cannot create an instance of that specific class. Only for inherited classes. Which I understand but creating a new instance of, say Feather like Feather feather = new Feather(); to get access to the method ListToys() doesnt make any sense when I want to get all types of toys from the list <Toy>.

Why they're abstract is part of the assignment to better understand inheritance of classes.

C#:
class Petowner
{
    public static void ListToys()
    {
        int i = 1;
        foreach (var toy in Toy)
        {
            if (Toy.OfType<Ball>().Any())
            {
                if (toy is Ball ball)
                {
                    Console.WriteLine(" {0} {1}", i, toy);
                    i++;
                }
            }
            else
            {
                Console.WriteLine(" Has no toys");
                break;
            }
       }
    }
}
 
Last edited:
There's a lot there that doesn't make sense. Firstly, this line:
C#:
foreach (var toy in Toy)
Why do you think that should do something useful? Look at your diagram for the PetOwner class. What propertiies does it have? Does it have a Toy property? No it does not, so what exactly do you think you're referring to in that line of code? It is the Animal class that has a Toy property, but that line of code is not in the Animal class or a class that inherits Animal, so there is no Toy property to be accessed. By the way, that property should be named Toys, as all collection properties should.

Next, even if there was such a property, this sequence makes no sense:
C#:
foreach (var toy in Toy)
{
    if (Toy.OfType<Ball>().Any())
    {
        if (toy is Ball ball)
        {
What are you actually trying to achieve there? Why would you check whether the Toy collection contains any Ball objects for every item in that same collection? If you had two balls and two bones, you would check whether there were any balls four times. How does that make sense? IIf what you are actually trying to do is get each Ball in the Toy collection then that would look like this:
C#:
foreach (var ball in Toy.OfType<Ball>())
{
    // Use ball here.
}
If you wanted to do that the more long-winded way:
C#:
foreach (var toy in Toy)
{
    if (toy is Ball ball)
    {
        // Use ball here.
    }
}
If what you are actually trying to do is list the toys for each pet owned by a pet owner then you might do this:
C#:
foreach (var pet in Pets)
{
    foreach (var toy in pet.Toy)
    {
        // Use toy here.
    }
}
 
Last edited:
Next, even if there was such a property, this sequence makes no sense:
C#:
foreach (var toy in Toy)
{
    if (Toy.OfType<Ball>().Any())
    {
        if (toy is Ball ball)
        {
I totally agree on every point. And when looking at the code provided it looks like a mess.
I refactored the foreach code before reading your post, however with something similar thank god. Often it feels like I'm getting blind by looking at code for too many hours and totally misses to think logically.

C#:
foreach (var animal in Petowner.Pets)
{
    foreach (var toy in animal.Toy)
    {
     
    }
}

Thanks a lot for your answers and input!
 
Back
Top Bottom