views:

77

answers:

4

Hello,

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

+1  A: 

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

David Relihan
I am using this now:A virtual inherited property CAN be overridden in a derived class by including a property declaration that uses the override modifier.Much better than sealed =>Base class:public virtual string DisplayName { get; set; } Sub class: private string _displayName; public override string DisplayName { get { return _displayName; } set { _displayName = value; } }@DAVIDNot a solution but you pointed me in the right direction there your point goes :P
msfanboy
You said for some reason but you do not know one ? you mean that seriously as answer? ;-)Hey its a string property displayname not more not 100000 LOC app.I want to keep it practical.
msfanboy
A: 

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.

Fede
A: 

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.

Nenad
introduce anotehr abstract class just for a property? come one...
msfanboy
A: 

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();
}
dbemerlin