Override a method from a base class that explicitly implements an interface

[Change]

Due to a bit more complex than was necessary at the original post - my apologies for this - I will try to simplify a bit.

Initially, I had a base class that looked like this:

public class OurViewModel { public bool Validate(ModelStateDictionary modelState){...} } 

My class inherited and hid this method using the new keyword:

 public class MyViewModel : OurViewModel { public new bool Validate(ModelStateDictionary modelState) {...} } 

Meanwhile, the IValidatableObject interface has appeared:

 public interface IValidatableObject { IEnumerable<ValidationResult> Validate(ValidationContext validationContext); } 

Now, OurViewModel (unfortunately) has also been changed. It looks like this:

 public class OurViewModel : IValidatableObject { IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext) {..} } 

Now I need to hide the Validate method in MyViewModel again, but I cannot figure out how to do this with the current layout. Is there a good way around this?

(FWIW, I agree that adding a new interface to the base class is not the best way to do something, but this is what happened here.)


[Original post]

I have problems with overriding a method in one of my classes. Someone changed the base class inheritance model:

 public class OurViewModel : IntlViewModelBase<OurResources> 

:

 public class OurViewModel : IntlViewModelBase<OurResources>, IValidatableObject 

IValidatableObject has the signature of one method, Validate() :

 IEnumerable<ValidationResult> Validate(ValidationContext validationContext); 

OurViewModel had a Validate method that looked like this:

 public bool Validate(ModelStateDictionary modelState) 

... and now this is after the change:

 IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext) 

My class, meanwhile, inherits from OurViewModel . He used the new keyword to implement his own boolean version of Validate.

It seems I can no longer hide the Validate method because my class does not implement IValidatableObject. What is the correct way to override this method now?

+4
source share
4 answers

I may be missing the point completely, but this compiles for me (using stub classes for OurResources and IntlViewModelBase<T> ).

 public class OurViewModel : IntlViewModelBase<OurResources>, IValidatableObject { public bool Validate(ModelStateDictionary modelState) { throw new NotImplementedException(); } IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext) { throw new NotImplementedException(); } } public class MyViewModel : OurViewModel { public new bool Validate(ModelStateDictionary modelState) { throw new NotImplementedException(); } } 
0
source

If you want to override the new Validate method (IValidatableObject.Validate), you just need to add the virtual keyword.

 public virtual IEnumerable<ValidationResult> Validate(ValidationContext validationContext) 
0
source

I think I have outlined this question in sufficient detail and decided to try again with the simpler version presented here: this

Thanks to all who responded. Can an administrator close this topic or aim / link it to a new question? I am not sure if SO netiquette is in such situations.

0
source

A few things:

1.

If for each method in the base class you want to have a new implementation in the derived class, then there is no point in deriving from the bottom. The main purpose of inheritance is code reuse. That way you can just remove the parent child relationship.

2.

Suppose there are some methods in your base class that actually inherit. In this case, I do not want the problem with marking the Base class Interface with a virtual keyword and in a derived class, saying this as new.

-

If you are asking about another way to implement this situation, we need to know what your goal is. If the goal is to get a member to have a new definition, then the best way would be to explicitly implement the interface in a derived class.

What is the specific problem you are facing? it’s that you cannot use the new keyword or you are setting different ways to solve this situation.

0
source

All Articles