How do I access properties of an object that is inside another object?

3

In the background I want this output Console.WriteLine(cidade1.casas.dono); to return João

using System;


namespace arrayteste
{

    public class cidade
    {
        public string nome { get; set; }
        public object casas { get; set; }
        public cidade(string nome, object casas)
        {
            this.nome = nome;
            this.casas = casas;
        }
    }
    public class casa
    {
        public string dono { get; set; }
        public string cor { get; set; }
        public casa(string dono, string cor)
        {
            this.dono = dono;
            this.cor = cor;
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Object[] casas =  new object[2];
            casa casa1 = new casa("João", "verde");
            casa casa2 = new casa("José", "vermelha");
            casas[0] = casa1;
            casas[1] = casa2;
            cidade cidade1 = new cidade("Lisboa", casas[0]);
            cidade cidade2 = new cidade("Porto", casas[1]);


            Console.WriteLine(cidade1.casas.dono); //João

            Console.ReadKey();

        }
    }
}

I want to access the properties of an object that are inside another object.

    
asked by anonymous 27.06.2017 / 14:47

2 answers

4

To solve this problem the way you've developed you have to do this:

((casa)(cidade1.casas)).dono

The problem is that casas is of type object and it does not have the dono member so it can not be accessed. An object can only access members that are of that type. You're embracing the concept of inheritance and polymorphism and even encapsulation. Every object in C # is derived from object therefore any object of any kind can be stored in it. Specifically it can be of another type since there is a subtype relation, but when you access it as object only the members of object are visible to you. For the code to see the other members, you need to cast cast to transform object into casa . Here the dono member is available for use. If you try to cast and it is not possible because the type is not compatible, that is, it is not an object casa will give error.

See the documentation for Object

I would make other modifications. It seems to me that the intention is up to another.

using static System.Console;

namespace arrayteste {
    public class Cidade{
        public string Nome { get; set; }
        public Casa Casas { get; set; }
        public Cidade(string nome, Casa casas) {
            Nome = nome;
            Casas = casas;
        }
    }
    public class Casa {
        public string Dono { get; set; }
        public string Cor { get; set; }
        public Casa(string dono, string cor) {
            Dono = dono;
            Cor = cor;
        }

    }
    public class Program {
        public static void Main(string[] args) {
            Casa[] casas =  new Casa[2] {
                new Casa("João", "verde"),
                new Casa("José", "vermelha")
            };
            Cidade cidade1 = new Cidade("Lisboa", casas[0]);
            Cidade cidade2 = new Cidade("Porto", casas[1]);
            WriteLine(cidade1.Casas.Dono);
        }
    }
}

See working on .NET Fiddle .

But I think you need to do something else. You almost always want to use a list ( List<> ) and not an array . It is more flexible and meets the needs better. This array in the code has no meaning whatsoever, it is useless and can be deleted.

But to what the class says Cidade must have a list of houses inside it, then one of the members must be a list. In case it would be useful to have a method to add houses. The class is still a bit naive, but it gets better.

I've changed the naming pattern for what C # adopts . And I simplified and modernized the code. If it's to learn, learn how to make real code today.

See:

using static System.Console;
using System.Collections.Generic;

namespace arrayteste {
    public class Cidade{
        public string Nome { get; set; }
        public List<Casa> Casas { get; set; } = new List<Casa>();
        public Cidade(string nome, Casa casa) {
            Nome = nome;
            Casas.Add(casa);
        }
        public void NovaCasa(Casa casa) {
            Casas.Add(casa);
        }
    }
    public class Casa {
        public string Dono { get; set; }
        public string Cor { get; set; }
        public Casa(string dono, string cor) {
            Dono = dono;
            Cor = cor;
        }

    }
    public class Program {
        public static void Main(string[] args) {
            var cidade1 = new Cidade("Lisboa", new Casa("João", "verde"));
            var cidade2 = new Cidade("Porto", new Casa("José", "vermelha"));
            cidade1.NovaCasa(new Casa("Joaquim", "azul"));
            WriteLine(cidade1.Casas[0].Dono);
            WriteLine(cidade1.Casas[1].Dono);
        }
    }
}

See running on .NET Fiddle . And no Coding Ground . Also I put it in GitHub for future reference .

    
27.06.2017 / 14:49
6

You are trying to access the property casas in object , this property does not exist in object .

You can do a cast

Console.WriteLine(((casa)(cidade1.casas)).dono).

But the correct thing is you change your classes to not use object , but rather use the class name correctly.

Of course it's possible to use object and keep casting casts back and forth, but I guess that's not what you want. It is not wrong, but it is very likely to cause problems, not to mention that doing this for no specific reason is incorrectly using the mechanism.

Another important thing is the naming and coding style, I find it important to follow the pattern adopted by C #. See more about this in this question.

Your code should look like this, see working in .NET Fiddle :

using System;   

namespace arrayteste
{    
    public class cidade
    {
        public string nome { get; set; }
        public casa casas { get; set; }
        public cidade(string nome, casa casas)
        {
            this.nome = nome;
            this.casas = casas;
        }
    }

    public class casa
    {
        public string dono { get; set; }
        public string cor { get; set; }
        public casa(string dono, string cor)
        {
            this.dono = dono;
            this.cor = cor;
        }

    }

    public class Program
    {
        public static void Main(string[] args)
        {
            casa[] casas =  new casa[2];
            casa casa1 = new casa("João", "verde");
            casa casa2 = new casa("José", "vermelha");
            casas[0] = casa1;
            casas[1] = casa2;
            cidade cidade1 = new cidade("Lisboa", casas[0]);
            cidade cidade2 = new cidade("Porto", casas[1]);


            Console.WriteLine(cidade1.casas.dono); //João


        }
    }
}
    
27.06.2017 / 14:51