Jump to content
  • Advertisement
Sign in to follow this  
bilsa

[.net] abstract Properties

This topic is 4439 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
Advertisement
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
Thank you anyway. It's hard to describe my actual application, right now I'm doing something similar to your second post. Though it would be much cleaner without it.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!