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 .