Why can't I add an override attribute access element?

In the base class, I have this property:

public virtual string Text 
{
    get { return text; }
}

I want to override this and return another text, but I would also like to be able to set the text, so I did this:

public override string Text
{
    get { return differentText; }
    set { differentText = value; }
}

This, however, does not work. I get a red squiggly under set, saying that I cannot override because it does not have an access set. Why is this a problem? What should I do?

+5
source share
5 answers

, "" . , .

ArsenMkrt , , . , - , , , , .

, ( , ), , , ( ), , , :

///<summary>
///Get the Text value of the object
///NOTE: Setting the value is not supported by this class but may be supported by child classes
///</summary>
public virtual string Text 
{
    get { return text; }
    set { }
}

//using the class

BaseClass.Text = "Wibble";
if (BaseClass.Text == "Wibble")
{
    //Won't get here (unless the default value is "Wibble")
}

:

public override string Text
{
    get { return differentText; }
}

public void SetText(string value)
{
    differentText = value;
}
+3
public virtual string Text 
{
    get { return text; }
    protected set {}
}

, , ,

+3

, . - , .

, , .

, . , .

+2

:

public new string Text
{
    get { return differentText; }
    set { differentText = value; }
}

,

+2

, . , , . , readonly Text, / .

:

protected string text;
public string Text 
{
    get { return text; }
}

:

new public string Text 
{
    get { return text; }
    set { text = value; }
}
+2

All Articles