How to: Have a property with ...

  • Thread starter Thread starter Roy Chastain
  • Start date Start date
R

Roy Chastain

I have an abstract base class Abc that has a read-only abstract property p1

public abstract class Abc
{
public abstract string p1
{ get; }
}

I have a derived class Dc that derives from Abc.
public class Dc: Abc
{
public override string p1
{
get { return "string"; }
}

So far so good.

The problem is that I would also like to have a writeable property that is available only to code that has visibility to dc while
code that only has visibility to the definition of Abc only sees the read part of the property.

example
Abc abc
Dc dc

dc.p1 = "dc"; // this would be valid
abc.p1 = "dc"; // this would be a compile error


What is the "correct" way to do this?

Thanks
 
Roy,

This isn't possible. You will have to create a method accessor that
would do this.

Your other option is to create a virtual implementation of the property
on the base level. Then, in the set implementation, you would throw an
exception. However, I don't know that this is what you want, since you
could take an instance of DC, cast it to ABC, and then set the property.

Hope this helps.
 
I had pretty much come to the conclusion that it was not possible just using C# properties. Thanks for confirming that.

Is there a 'normal' pattern for doing this sort of thing?

The most obvious would be as you suggest a method defined only in the derived class that could set the value of the field that the
property accesses.

Thanks
 
This is one place where C++/CLI scores over C#, it allows access
specifiers for get and set, individually.

Regards
Senthil
 
Senthil,

C# 2.0 allows that as well, but that's not the issue here. The issue
isn't the access specifier, it's about ^adding^ a set function to an
abstract property declaration which only has a get. I don't believe that
CLI will let you do that either (because now the property definition changes
from readonly to readwrite).
 
Yeah, you're right. What mislead me was the OP's statement "The problem
is that I would also like to have a writeable property that is
available only to code that has visibility to dc while
code that only has visibility to the definition of Abc only sees the
read part of the property. ".

I took that to be different access specifiers for get and set. My bad.

Regards
Senthil
 
Roy,

There is nothing wrong with the way you worded the question, these
concepts are not exactly easy to convey, either way. The way you worded it
and Senthil's mistake are both fine, neither was really wrong.
 
You can't do it with an abstract property in the base class. However,
you can do it with a property that has an implementation, using "new":

public abstract class AbstractBase
{
private int _field2;

public AbstractBase()
{
this._field2 = 2;
}

public abstract int prop1
{
get;
}

public virtual int prop2
{
get { return this._field2; }
}

protected void setProp2(int newValue)
{
this._field2 = newValue;
}
}

public class Derived : AbstractBase
{
private int _field1;

public Derived()
{
this._field1 = 1;
}

public override int prop1
{
get
{
return this._field1;
}
}

public new int prop2
{
get { return base.prop2; }
set { base.setProp2(value); }
}
}

I know this isn't what you asked, but....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top