Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

8
Can I make a property have a private setter in a sub class? Or, what would be the best way to impliment this?
Post Body

super class, where I want to be able to freely change Viable:

namespace Life
{

class Egg
{
    public double Size { get; private set; }
    public string Type { get; private set; }

    public virtual bool Viable { get; set; } = true;

    public Egg(string type, double size)
    {
        this.Type = type;
        this.Size = size;
    }
}
}

sub class, where I want Viable to always be false:

namespace Life
{
internal class BrokenEgg : Egg
{
    public override bool Viable { get; set; } = false;
    public BrokenEgg(string type) : base("broken "   type,0)
    {
    }
}
}

I cannot change Viable to a private setter in the sub class, but I want to (a broken egg would never be viable). I suppose I could use a private field for Egg and have the setter use that.

internal class BrokenEgg : Egg
{
    private bool _viable = false;
    public override bool Viable { 
        get { return _viable; }
        set {  } 
    }
    public BrokenEgg(string type) : base("broken "   type,0)
    {
    }
}

Alternatively, I could have the getter for BrokenEgg simply return false.

    public override bool Viable { 
        get { return false; }
        set {  } 
    }

I'm just not really sure what's the best idea here. Both of the solutions I have would let you do something like BrokenEgg.Viable = true; and everything would compile and you'd think the code worked fine.

the third option, and my favorite, is to put in an error myself:

    public override bool Viable { 
        get { return false; }
        set { throw new InvalidOperationException("you cannot set a BrokenEgg's Viabilility"); } 
    }

I actually think I really like this idea because I could explain the reasoning and it makes me explain the reasoning, too. It doesn't just look like a technical error.

Author
Account Strength
100%
Account Age
15 years
Verified Email
Yes
Verified Flair
No
Total Karma
639,966
Link Karma
107,616
Comment Karma
529,103
Profile updated: 3 days ago
Posts updated: 3 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 years ago