简体   繁体   中英

C# - What should I do when every inherited class needs getter from base class, but setter only for ONE inherited class

I have a abstract class called WizardViewModelBase.

All my WizardXXXViewModel classes inherit from the base abstract class.

The base has a property with a getter. Every sub class needs and overrides that string

property as its the DisplayName of the ViewModel.

Only ONE ViewModel called WizardTimeTableWeekViewModel needs a setter because I have to set

wether the ViewModel is a timetable for week A or week B. Using 2 ViewModels like

WizardTimeTableWeekAViewModel and WizardTimeTableWeekBViewModel would be redundant.

I do not want to override the setter in all other classes as they do not need a setter.

Can I somehow tell the sub class it needs not to override the setter?

Or any other suggestion?

With interfaces I would be free to use getter or setter but having many empty setter

properties is not an option for me.

Funny.. I have just thought what would happen if I really would need to SET all DisplayNames of the WizardPages contrary what I said firstly. Maybe I should not hardcode the strings in the getter and put the strings in a reesource file because of localization, then I need a setter anywhere in every sub class XD

Don't declare the setter method as virtual.

If for some reason (I can't think of one!) you need for it to be virtual at the top of your inheritance hierarchy then use sealed when you override it:

http://msdn.microsoft.com/en-us/library/aa645769(VS.71).aspx

If the property is not abstract, then any base class may choose to only override the setter, the getter, or both.

If you want your subclasses not to have access to your setter, except for only a given subclass, you can use the internal access modifier only to the getter, and implement classes that shouldn't have access to the setter in another assembly.

You should introduce a new abstract class, which will inhere WizardViewModelBase class. That class should override a property using both get and set accessors, but will leave a property abstract, like this:

    public abstract string DisplayName 
    {
        get;
        set;
    }

Then you can use this class as a base class for WizardTimeTableWeekViewModel class and you wil be able to override both get and set accessors.

I'd use a protected setter and create a seperate function to set the value. After all the class does not have the same interface as the others so distinguishing it visibly from the others should help readability.

class Base
{
  public String Value { get; protected set; }
}

class SpecialChild : Base
{
  public void SetValue(String newValue) { this.Value = newValue; }
}

// Somewhere else
SpecialChild special = foo as SpecialChild;
if (special != null)
{
  special.SetValue('newFoo');
}
else
{
  foo.DoSomeStuff();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM