Removing inherited properties from an object.

  • Thread starter Thread starter UJ
  • Start date Start date
U

UJ

I have a text object (see my previous post) that I've added some
functionality to. But I don't want the user to be able to change the text
without calling my routines. So I added the following code:


public override string Text
{
get
{
return base.Text;
}
}


but I can still do something like testobject.text = "Fred"; and it
works. How can I make that not valid?

TIA - Jeff.
 
UJ said:
I have a text object (see my previous post) that I've added some
functionality to. But I don't want the user to be able to change the text
without calling my routines. So I added the following code:
public override string Text
{
get
{
return base.Text;
}
}


but I can still do something like testobject.text = "Fred"; and it
works. How can I make that not valid?

Well, the first thing is that you shouldn't do this. If someone can't
use your object like a TextBox, then your class shouldn't inherit from
TextBox - see Liskov's Substitutability Rule.

However, you could possibly override the setter as well as the getter,
and make it throw an exception. That's only going to be a runtime error
rather than a compile-time error though.

Jon
 
The name of your property is Text, the T in Text for your property name
is capital. You are trying to set the testobject.text, so "text" is not
the same as the "Text" property that you overrode.

In order to make testobject.text not react to changes, you need to:
1. change the accessibility of the text member variable of the base
class to non-public [this is sometimes not possible as the base class
might have been provided by 3rd party of the framework], or
2. enforce your clients to use the Text property and ask them to
refrain from using the text variable [hard to verbally ask them to
prevent this], or
3. before passing to the base class for any of the routines, make sure
you reset the text variable from what was set for the Text property,
eg.

protected string myText;

public override string Text
{
get
{
return myText;
}

set
{
myText = value;
base.text = myText;
}
}

public override Load(object sender, EventArgs e)
{
base.text = myText;
base.Load(sender, e);
}

I hope this helps.

Sincerely,
Bobby
 
Jeff,
Jon's comment about changing the expected usability of an object is sound.
The basic technique here, however, would be to override the settor as well as
the getter in the property:


public override string Text
{
get
{
return base.Text;
}
set
{
// your cool code here...
}
}

-- Peter
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
"UJ" <[email protected]> a écrit dans le message de (e-mail address removed)...

|I have a text object (see my previous post) that I've added some
| functionality to. But I don't want the user to be able to change the text
| without calling my routines. So I added the following code:
|
|
| public override string Text
| {
| get
| {
| return base.Text;
| }
| }
|
|
| but I can still do something like testobject.text = "Fred"; and it
| works. How can I make that not valid?

You can only guarantee not to call a property setter if you don't declare
it.

Trying to hide functionality from a base class is a sign of bad design,
sorry :-(

You can attempt to hide the property setter, but it will still be accessible
from any code that treats the object as if it were an instance of the base
class.

e.g.

{
BaseText bt = new DerivedText();
bt.Text = "Fred";
}

....will always work according to the base class's setter method, unless the
property is marked as virtual in the base class.

You can try to hide the base setter by declaring the dreived class's
property as "new"; as long as nothing else knows it as the base class, then
you should be OK.

class DerivedText : BaseText
{
public new string Text
{
get { return base.Text; }
set
{
// different code that calls your routines
}
}
}

Joanna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top