If you want to take a property, you have to instantiate a class that has this property. So if you want the CPF
, needs to instantiate the DadosCLientesPF
. Instantiate DadosClientes
there is no way to get the number simply because it does not exist in this class.
If you are using a method you receive DadosClientes
and needs to access something that is not available on it, is doing something wrong as well. In this case the correct would be to have separate methods to deal with DadosClientesPF
, with DadosCLientesPJ
and maybe even keep what deals only with the DadosClientes
if it’s useful, which I doubt.
Especially in the taste of the solution given in the other answer. I am not a purist but that is not object-oriented programming. Methods should not have to know how to handle derivative objects of the expected type. When this is necessary, specialized methods should be created, otherwise if it is later created ClientesDadosEstrangeiro
would have to tinker with the implementation of this method. Depending on the scenario it may not be problematic but in others it may be. I don’t know if you need to use inheritance in this case, but if you’re gonna use it, do it for the right reasons, and use it the right way.
If you have separated into two classes, treat them as different things. If the intention is to treat them as if they were the same thing, do not separate.
But if your problem is polymorphism (not that it is the case), that is, you are instantiating the derived class but when you pass it to some method that expects the base class, then your problem is that the property needs to be virtual. This guarantees the polymorphism and will call the property correctly, since it will interpret the object with its type originally instantiated and not by the type concretely declared in the method parameter. Of course this only applies if the property exists in the base class.
Maybe you need reevaluate all your modeling.
Could you provide the code so we can help you more precisely? I suppose you’re using inheritance to compose right Customer, Data, Data?
– Erick Gallani
Creating an instance of
DadosClientesPF
orDadosClientesPJ
. You cannot access what does not exist. If the problem is not just that, explain it better and put some example.– Maniero
@Kleberbarros, as the bigown pointed out, I don’t think it’s a good idea to box and Unbox
DadosClientePF
andDadosClientePJ
. In any case, I advise you to read more about inheritance models in EF: Table-per-Type (TPT), Table-per-Hierarchy (TPH) and Table-per-Concrete (TPC). http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines– Tobias Mesquita
@Tobymosque Box and Unbox is a terminology used for value-types. You were probably referring to type-Casts between base and derivative classes.
– Miguel Angelo
Fault mine, thanks for the correction.
– Tobias Mesquita
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero