0

I have a problem with an accessor inherited, I can't define the set method.

My code :

public abstract class MotherOfDragons
{
    public abstract String DragonsName { get; }
}

the classes inherited :

public class Drogon : MotherOfDragons
{
    public override String DragonsName { get; set; }
}

public class Viserion : MotherOfDragons
{
    public override String DragonsName { get; }
}

It's works goo for Viserion but Drogon I have the error CS0546

'accessor' : cannot override because 'property' does not have an overridable set accessor

It is possible solve this error without add set accessor in class MotherOfDragons ? I want to keep this field read only expect for one case.

Thank you

A.Pissicat
  • 3,023
  • 4
  • 38
  • 93
  • 2
    http://stackoverflow.com/questions/2026546/override-get-but-not-set – sircodesalot Jun 30 '16 at 13:50
  • Is it a solution to use a interface isntead of an abstract class. if you change: public interface MotherOfDragons and then remove the overrides – tire0011 Jun 30 '16 at 13:55
  • I can't do `MotherOfDragons` as Interface, I implement some methods inside and it inherit from an other class – A.Pissicat Jun 30 '16 at 13:58
  • It's somewhat bad design. Typically if you have `abstract class`, then there will be some functionality (otherwise it should be an [interface](http://stackoverflow.com/q/747517/1997232), e.g. if you plan to have `List` to hold different inherited dragons). If your problem is to set name when constructing inherited object, then simply add `protected` setter or method to base class. I think `public abstract string DragonsName { get; protected set; }` is more appropriate here. – Sinatr Jun 30 '16 at 14:40

1 Answers1

0

Thank you @sircodesalot

I finally made this :

public class Drogon : MotherOfDragons
{
    private String dragonsName;
    public override String DragonsName { get { return dragonsName; } }

    public void Change_DragonsName(String name)
    {
        dragonsName = name;
    }
}

It works, not like a set, but it works.

A.Pissicat
  • 3,023
  • 4
  • 38
  • 93
  • If you don't want to change base value, you can simply do `public new string DragonsName { get; set; }` (no need for a field or method). – Sinatr Jun 30 '16 at 14:03
  • I have try this at first but it return an error `cs0534 class does not implement inherited abstract member` – A.Pissicat Jun 30 '16 at 14:19
  • Ouch, my bad. Missed `abstract` base class (for normal `virtual` property that would work). – Sinatr Jun 30 '16 at 14:34