C # - What if each inherited class needs a getter from the base class and setter only for the ONE of the inherited class

I have an abstract WizardViewModelBase class.

All WizardXXXViewModel classes inherit from the base abstract class.

The base has a getter property. Each subclass requires and overrides this string.

like its DisplayName ViewModel.

Only an ONE ViewModel named WizardTimeTableWeekViewModel needs a customizer because I need to install

so the ViewModel is a schedule for week A or week B. Using 2 ViewModels such as

WizardTimeTableWeekAViewModel and WizardTimeTableWeekBViewModel will be redundant.

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

Can I somehow tell the subclass that it does not need to redefine the setter?

Or any other suggestion?

With interfaces, I could freely use getter or setter, but having many empty setters

properties are not suitable for me.

Funny .. I just wondered what would happen if I really needed to INSTALL all the WizardPages display names contrary to what I said. Maybe I don't need to hardcode the lines in the getter and put the lines in the reesource file due to localization, then I need a setter anywhere in every XD subclass

+1
source share
4 answers

Do not declare the setter method as virtual.

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

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

+1
source

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

If you want your subclasses to not have access to your network device, except for only a certain subclass, you can use the internal access modifier only for the recipient and implement classes that should not have access to the installer in another assembly.

0
source

You should introduce a new abstract class that will contain the WizardViewModelBase class. This class should override the property using both get and set accessors, but will leave an abstract property, for example:

public abstract string DisplayName { get; set; } 

You can then use this class as the base class for the WizardTimeTableWeekViewModel class, and you can override both get and set accessors.

0
source

I would use a secure setter and create a separate function to set the value. After all, the class does not have the same interface as the others, so its excellent difference from 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(); } 
0
source

Source: https://habr.com/ru/post/969769/


All Articles