Is it possible to override a base class property?

Asked

Viewed 325 times

5

I have a class Cliente, which is the basis. In it I have the property Celular with Data Annotation "Required".

I wonder if it is possible to use a property Cellular in class ClientePersonalizado, inherited from the base class Cliente, but without taking the Required base class in the Cellular property.

Client class:

public class ClienteViewModel
{
    [Required(ErrorMessage = "Preencha o {0} do Cliente")]
    public string Celular { get; set; } 
}

Class Custom Clientele:

public class ClientePersonalizadoViewModel : ClienteViewModel
{
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public string Celular { get; set; }
}

I tried to use the properties as virtual and override, but it didn’t work. It is possible to use in this way?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

3 answers

7

Yes, it is possible. First make the property virtual:

public class ClienteViewModel {
    [Required(ErrorMessage = "Preencha o {0} do Cliente")]
    public virtual string Celular { get; set; } 
}

Then I’d overwrite it:

public class ClientePersonalizadoViewModel : ClienteViewModel {
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public override string Celular { get; set; }
}

I put in the Github for future reference.

Care, new does not overwrite anything. See: How "new" and "virtual" work in C#?. It’s the only way to override, shadow the property with new will not give the effect you want.

And the attribute goes together, as it is ready has nothing to do, unless you create your own attribute and do not allow inheritance (I’m not sure how it works).

On the other hand, if the property does not have the same attribute, is it an inheritance there? Are you using the mechanism correctly? Do you know the liskov principle? The solutions I see out there is not to use inheritance when you have a case like this (example).

I need to see if you can disable the attribute in any way. It has attributes that you can change its state, even though it is still present.

  • É a única forma de fazer a sobrescrita, sombrear a propriedade com new não dará o efeito que deseja. that’s not true ...

  • 3

    So you don’t understand what the new makes. Read the answer, I talk about inheritance and superscript. if the new solved the superscript so everyone wore it and wouldn’t really need it. There’s a case that it works because you don’t want to overwrite, where it’s needed it doesn’t work, and that’s why there’s something separate.

  • You must be the only one who understands here is you the guy who knows everything about C# and closes all the C# questions and answers the one that interests you the most ... Unfortunately Stackoverflow has become a joke in certain tags. this is a ... Where have you ever seen to affirm something without testing ... if tested ??? I guess not.

  • I tested it, you didn’t, otherwise you’d know it doesn’t happen. If Stack Overflow is that bad, I think you should review your participation here. If there is something you disagree with, you can always say it in the comments, and depending on the case lead to the goal. Just don’t forget that you shouldn’t attack people, just disagree. And make arguments. Just saying you don’t agree doesn’t do much good. A recurring fact is that your disagreement is not accepted because you lack arguments and you attack the person by saying exactly what you always do that is not accepting the opinion.I passed your case to other moderators

  • Sincerity, I doubt you tested ... but, all right, as I’ve reported you can be the master of wizards in C# ... Oh another thing you can report me to the owner of the site, to whoever you want, if it was disrespectful to your person that they come talk to me or else that they make the laws of the site, but, I hope that the Law is made for both sides.

  • Guys, I forgot to mention one important thing. It didn’t work with virtual /override or new. What happens is that in Modelstate.Isvalid, you are validating the required base class.

  • @Pedrotomaz so I said the real solution to what you want is not to make inheritance.

  • Good @Maniero. I was thinking and remove the inheritance, however I would have to modify a lot of the project structure. In my case, I managed to put in the child class, the date Annotation [Required(Allowemptystring = true)], as I left as a response. Thanks.

  • @Pedrotomaz that is what I was going to see in the doc, but I ended up distracted with the discussion above and forgot.

  • I guess nobody tested it after all, huh? Rsrsrs

  • @Victorlaio I tested the question of the superscript, and said what gives and what can not do. And I knew there was a way parameterizing the attribute, I just didn’t have time to post

  • Conclusion then is that whenever a property is overwritten, its data annotations remain the same as the property that overrides?

  • 1

    @Victorlaio you cannot remove them, but you can change their state, as it is in the answer, and some of these attributes were made to be able to change the behavior, as this for example,, so if you want an attribute not to exist the only normal way is not to do inheritance. It may be that I have some form of reflection to do this, I would need to investigate, but it is Gambi anyway. To tell you the truth I don’t know if the solution used by AP is a good one, I still need to understand all the consequences of this, it may work, but it may not be the right one, so I haven’t validated it yet.

  • It’s that story, most people just want to see it work and not be right so I put in the answer that probably the solution is different. I understand that AP doesn’t want to change, but there is that thing https://i.stack.Imgur.com/g0Toi.png

  • Now that I’m learning C# I’m enjoying your answers. kkkkk And I wonder why I haven’t learned it before. C# teaches real programming. Nothing against other languages, on the contrary, they complement each other.

Show 10 more comments

4

  • 1

    Perfect, and makes the code more elegant.

1

I forgot to mention something important. It didn’t work with virtual /override or new. What happens is that in Modelstate.Isvalid, you are validating the required of the base class and giving error because it is a required property.

I managed to solve by placing [Required(Allowemptystring = true)] in the daughter class:

public class ClienteViewModel {
   [Required(ErrorMessage = "Preencha o {0} do Cliente")]
   public virtual string Celular { get; set; }
} 

public class ClientePersonalizadoViewModel : ClienteViewModel
{
   [Required(AllowEmptyString = true)]
   [DisplayFormat(ConvertEmptyStringToNull = false)]
   public string Celular { get; set; }
}

Valeu class.

  • It has nothing to do with what you said, and something else virtual and override is not virtual and new the same is in your answer ... The two forms work one being a extends and the other is to hide. Since we don’t see a real example in practice it is difficult to say why it worked now. But dude ... keep going since it’s no use talking too much.

  • Another factor if you put equal to your answer then my answer answers your doubt because the modifier new is used to hide the member of the Father class and that’s what happened.

  • @Virgilionovic the new is wrong anyway. I’ve edited the answer and it’s now correct in the way it worked for me. Your remark was of great help. Thanks.

  • My God man if you must be going crazy ... I said one thing and you did another ... but, man still in your footsteps there ... Congratulations .

  • Clearly you haven’t noticed the answers, of course it’s fact. You’re going by the lucky factor.

Browser other questions tagged

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