How to access properties of an object that is inside another object?

Asked

Viewed 904 times

3

Deep down I want this output Console.WriteLine(cidade1.casas.dono); returns 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 inside another object.

2 answers

6

You’re trying to access the property casas in a object, this property does not exist in object.

You can make a cast

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

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

Of course it is possible to use object and keep doing Casts Back and forth, but I don’t think that’s what you want. It is not wrong, but it is very likely to give problems, not to mention that to do this without specific reason is to incorrectly use the mechanism.

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

Your code should be as below, see working on . 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


        }
    }
}

4


To solve this problem the way you’ve developed it has to do this:

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

The problem is that casas is the type object and he does not possess the member dono so it can’t be accessed. An object can only access members that are of that type. You’re sitting on the concept of inheritance and polymorphism and even encapsulation. Every object in C# is derived from object so any object of any kind can be stored in it. Concretely it may be of another type since there is relation of subtype, but when accesses it as object is only visible to you members of object. For the code to see the other members you need to make one cast to transform the object in casa. There the member dono is available for use. If you try to cast and is not possible because the type is not compatible, that is, it is not an object casa make a mistake.

See the documentation of 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);
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

But I think you need to do other things. Almost always you want to use a list (List<>) and not a array. It is more flexible and better meets the needs. This array in the code has no sense whatsoever, it serves no purpose and can be deleted.

But what makes the class seem Cidade should have a list of houses inside it, so one of the members should be a list. In case it would be useful to have a method to add houses. The class is still a little naive, but it gets better.

I changed the nomenclature standard for what C# adopts. And I’ve simplified and modernized the code. If you want to learn how to make real code today.

Behold:

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

namespace ListTeste {
    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);
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • I want to access the properties of an object that is inside another object. That’s right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.