Question Display elements from List<T> using derived class and function

chucki21

New member
Joined
Jul 12, 2022
Messages
3
Programming Experience
Beginner
Hi,

I'm noob in C# and I tried to display elements from List<T> using the following way:
It's possible to see element from List using this way ? (derivate class based inheritance).
Right now after I run this code the List is empty, of course if I move method Show() inside Student class all is good but i don't want.to use this way.

Thank you!



C#:
        class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }

        class Student
        {
            public List<Person> li = new List<Person>();
            
            public void Add(Person p)
            {
                li.Add(p);
            }
        }

        class Tools : Student
        {
            public void Show()
            {
                foreach (Person p in li)
                {
                    Console.WriteLine($"Display: [Name {p.Name[0]} and age {p.Age}");
                }
            }
        }
        
        static void Main(string[] args)
        {
            Person p1 = new Person () { Id = 1, Name = "Xxxxx",  Age = 33 };
            Person p2 = new Person () { Id = 2, Name = "Yyyyy",  Age = 66 };
            Person p3 = new Person () { Id = 3, Name = "Zzzzz",  Age = 99 };

            
            Student e3 = new Student();
            e3.Add(p1);
            e3.Add(p2);
            e3.Add(p3);

            Tools t= new Tools();
            t.Show();

        }
 
You seem to think that a new Tools object has some magic connection to an existent Student object. It doesn't. Based on your code, a Tools object IS a Student object, so you need to create a just a Tools object, and the Person objects to that, then show them:
C#:
Tools t = new Tools();

t.Add(p1);
t.Add(p2);
t.Add(p3);

t.Show();
 
You seem to think that a new Tools object has some magic connection to an existent Student object. It doesn't. Based on your code, a Tools object IS a Student object, so you need to create a just a Tools object, and the Person objects to that, then show them:
C#:
Tools t = new Tools();

t.Add(p1);
t.Add(p2);
t.Add(p3);

t.Show();

Thank you for answer
i assumed that i have access to my List from Student class using inheritance (derivate class)
Inheritance doesn't mean the idea of having access to the elements that form the basics class ?
..so using this itsn't possible to access List ?

class Tools : Student
{
 
Inheritance means that a type has all the functionality of the base class plus any functionality it adds itself. It doesn't mean that a class has magical access to every instance of its base class. If you created one Student object, added Person objects to it and then created a second Student object, would you expect to be able to access those Person objects that you added to the first Student object via the second Student object? I would hope not, so why would you expect a Tools object to be able to access those Person objects that you added to the Student object?

Like I said, the Tools class inherits the functionality of the Student class, which means that a Tools object has an li field and an Add method. It doesn't mean that the li field of a Tools object magically contains data that you add to the li field of a different Student object.

The point of OOP is to mimic the way objects work in the real world. A truck is a vehicle. If you were to get a vehicle and put petrol in it, then get a truck, would you expect to be able to access that petrol via that truck? I hope not, because that would be magic.
 

Latest posts

Back
Top Bottom