Sign in to follow this  

[.net] abstract Properties

This topic is 4068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ohoy! I was wondering if there is any way of making this code work:

abstract class Base {
  abstract int MyProp { get; }
}

abstract class Derived {
  new abstract int MyProp { get; set; }
}

class Impl
  : Derived
{
  override int MyProp { get { return 0; } set {} }
}
The compiler says that Base.MyProp.get is not implemented. And it seems like the following is only viable for interfaces:
class Impl
  : Derived
{
  int Base.MyProp { get { return 0; } }
  override int MyProp { get { return 0; } set {} }
}
Thanks in advance!

Share this post


Link to post
Share on other sites
The problem is that there are actually 2 'MyProp' properties in Impl which need to be implemented, Base.MyProp and Derived.MyProp (this is because you've used the 'new' keyword in Derived). You've only provided an implementation for Derived.MyProp since it's the one visible in the current scope, hence the error about no implementation for Base.MyProp. This should work:


class Impl : Derived
{
override int MyProp { get { return 0; } set {} }
override int Base.MyProp { get { return Impl.MyProp; } }
}

Share this post


Link to post
Share on other sites
Nevermind, I just tried it in the compiler and I now see you're problem with it not working with abstract classes. I was able to get it working with an intermediate 'helper' class, but it's horribly ugly. Is there a particular reason you're not using interfaces here anyway?


abstract class Base
{
public abstract int MyProp { get; }
}

abstract class DerivedHelper : Base
{
protected abstract int MyPropHelper { get; set; }
public override int MyProp { get { return MyPropHelper; } }
}

abstract class Derived : DerivedHelper
{
public new abstract int MyProp { get; set; }
protected override int MyPropHelper { get { return MyProp; } set { MyProp = value; } }
}

class Impl : Derived
{
public override int MyProp { get { return 0; } set { } }
}

Share this post


Link to post
Share on other sites
Since Base only calls for one property, wouldn't it be better as an interface?


interface IBase
{
int MyProp { get; } // only get
}

public abstract class Derived : IBase
{
public abstract int MyProp { get; set; } // both get AND set
}

public class Impl : Derived
{
public override int MyProp { get { return 0; } set { } }
}

Share this post


Link to post
Share on other sites

This topic is 4068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this