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.
– Amadeu Antunes