1

I tried to create a new class base on a class, and I want to override a property in the base class for remove [Required]. But instead of overriding, it created a new property with the same name. So what wrong, and how to fix it.

namespace admin.models
{
    public class BaseClass
    {
        [Required]
        public string id { get; set; }
        [Required]
        public string name { get; set; }
    }

    public class NewClass : BaseClass
    {
        public new string name { get; set; }
    }
}

enter image description here

JimmyN
  • 579
  • 4
  • 16
  • 3
    You need to declare the property `virtual` or `abstract` in the base class, and `override` in the derived class. Base: `public virtual string id { get; set; }`, Derived: `public override string id { get; set; }` – Matthew Watson Jun 07 '19 at 09:55
  • In this case, I would use `abstract` – Ronald Haan Jun 07 '19 at 09:56
  • In your method add the line `var s = newClass.name;` and see what value is assigned to `s`. What you're doing is fine. – Reinstate Monica Cellio Jun 07 '19 at 09:57
  • @Archer But if he does `Baseclass b = newClass; var s = b.name;` he would get the base class version. – Matthew Watson Jun 07 '19 at 09:58
  • @MatthewWatson Yes he would, but that's not what he has. Understanding the difference is most likely worth the OP knowing. – Reinstate Monica Cellio Jun 07 '19 at 10:00
  • With that in mind, @jimmy, have a read of this -> **[Knowing When to Use Override and New Keywords](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords)** – Reinstate Monica Cellio Jun 07 '19 at 10:03
  • @Archer in below example, newClass.name = 'abcd',and when I post with name is null, ModelState.IsValid = false, what happen here – JimmyN Jun 07 '19 at 10:17
  • **[Have a look at this example](https://dotnetfiddle.net/kIn4x2)**. It all depends on how you refer to the object instance. Notice in that example I use the same object twice, but get different results because the functions take different type parameters. – Reinstate Monica Cellio Jun 07 '19 at 10:24
  • @Archer: my purpose is to remove [Required], I post NewClass from a client. What will happen in this case? – JimmyN Jun 07 '19 at 10:34
  • You're being very, very vague. If you want to remove the required attribute then remove it. Deal with the actual problem, rather than creating others. Sorry - can't help if I don't know what you're trying to do. Put a variable and breakpoint in the method and look at the value, like I said in my first comment. – Reinstate Monica Cellio Jun 07 '19 at 10:35
  • 1
    I would strongly suggest not using inheritance here. – mjwills Jun 07 '19 at 10:35

1 Answers1

1

You need to mark the method/property as virtual if you want to make it possible to override it. Check out this link for more info. Then use override to give a new implementation for the property.

namespace admin.models
{
    public class BaseClass
    {
        [Required]
        public string id { get; set; }
        [Required]
        public virtual string name { get; set; }
    }

    public class NewClass : BaseClass
    {
        public override string name { get; set; }
    }
}
iSpain17
  • 2,502
  • 3
  • 17
  • 26
  • I'm not gonna downvote because this is probably what the OP actually wants, but I can't upvote either because `You need to mark the method/property as virtual if you want to make it possible to override it` is not correct. – Reinstate Monica Cellio Jun 07 '19 at 10:25