In a base class, can I make one property accesser abstract, but put code in the other one?

  • Thread starter Thread starter 0to60
  • Start date Start date
0

0to60

I have a base class that defines a property. I'd like the the Get to be
implemented by the base class and not overrideable. I would like the Set to
be marked abstract, and thus needs to be filled in by overriding classes.
Is this possible? What's the syntax?
 
The short answer is "no", however ....

did you know .... your

string MyProperty { get { /* */} set{ /* */} }

is converted into

string get_MyProperty() { }
void set_MyProperty(string value) { }

pretty much the same way COM properties are translated, so .... why not
provide methods in the first instance, that way you can do the following ...

public sealed string MyProperty { get { } }
public abstract void SetMyProperty(string value) { }

for example
 
That not possible I'm afraid. You could provide a abstract and seperate Set
method.

You could have a protected Set method, and a sealed property, but have your
property call the set method. Like this...

public int MyProp
{
get
{
return myInt;
}
set
{
SetMyInt( value );
}
}

protected abstract SetMyInt( int theInt )
{
//Set the int
}

This way, from the public interface perspective, the derived class will
behave as if it had a polymorphic Set on the property.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
 
Back
Top