[.net] abstract Properties

Started by
3 comments, last by Rob Loach 17 years, 5 months ago
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!
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; } }}
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
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 { } }}
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
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.
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 { } }}
Rob Loach [Website] [Projects] [Contact]

This topic is closed to new replies.

Advertisement