"Hiding" an inherited property?

  • Thread starter Thread starter Jacob
  • Start date Start date
J

Jacob

Is there a way to make a property of an inherited class invisible to the
programmer? I know that using the keyword "new" allows you to create
another property in the place of the existing one, but how can I make the
property hidden and not available at all?


Thanks,
Jacob
 
Jacob,

I believe you're trying to re-write the fundamentals of inheritance by doing
this.

The official way is to raise an NotImplemented exception within your
handler.

Imagine the following scenario:

class MyBase
{
public virtual void DoSomething()
{
}
}

class MySubclass
{
public overrides void DoSomething()
{
throw new NotImplementException();
}
}

even if you could make the MySubclass::DoSomething invisible it would still
be visible through it's super class

e.g.

MyBase mb = new MySubclass();
MyBase.DoSomething();

This is why you need to raise the NotImplementException to prevent calling
this no matter where in the class hierachy that function is called from.

hope that clears it up,

cheers,

g

PS I've wanted to do the same sometimes but you've no choice but to either
subclass a class further up in the food chain or possibly use encapsulation
rather than inheritance to provide the functionality you require.
 
Ya, I've never found a way either, I was just hoping someone else knew one.
Using the NotImplemented exception is a good practice, thanks for the tip.
I could swear that Microsoft has done it on some of their classes though. I
can't think of a particular example right now, but I'm certain that I've
seen it somewhere.

Thanks,
Jacob
 
I think you can also write something like

public MyClass
{
public virtual string MyProp
{
get{...}
set{...}
}
}

public MyDerivedClass : MyClass
{
private new string MyProp
{
get{ return String.Empty; }
set{ /* do nothing */ }
}
}

HTH
 
Jacob said:
Is there a way to make a property of an inherited
class invisible to the programmer? I know that
using the keyword "new" allows you to create
another property in the place of the existing one, but
how can I make the property hidden and not available
at all?

I don't think you can, and it would be a very bad thing to do from an
object-oriented standpoint.

For it to be possible to treat any inherited class instance as though it
were a base class instance, the inherited class must have all of the members
of the base class.

P.
 
Jacob said:
Is there a way to make a property of an inherited class invisible to the
programmer?

No, fortunately. That would break Liskov's Substitutability Principle -
basically, whatever you can do with a base class, you should be able to
do with a derived class.
 
Mauro Servienti said:
I think you can also write something like

public MyClass
{
public virtual string MyProp
{
get{...}
set{...}
}
}

public MyDerivedClass : MyClass
{
private new string MyProp
{
get{ return String.Empty; }
set{ /* do nothing */ }
}
}

That doesn't do anything though, as far as other classes are concerned
- the fact that it's private means that it's basically irrelevant to
anything beyond the class itself (and any nested types). You could use
a *public* new property to hide the old one, as Jacob had already said,
but that's not what he was after.
 
I agree that it's important law of object oriented design that must be
adheard to. If the rules could be bent here or there, then the design of
the whole system would fall appart.

It's just that sometimes, properties no longer seem applicable in some
cases. The next best thing is if the class is a control and can be editted
by the designer, you can at least set an attribute to make it invisible to
the designer.

Jacob
 
Jacob said:
I agree that it's important law of object oriented design that must be
adheard to. If the rules could be bent here or there, then the design of
the whole system would fall appart.

It's just that sometimes, properties no longer seem applicable in some
cases. The next best thing is if the class is a control and can be editted
by the designer, you can at least set an attribute to make it invisible to
the designer.

Ah - you *can* do that, I believe, although I can't remember what the
attribute is. Presumably you can then override the existing property
(and maybe throws an exception) and apply the attribute to the
override.
 
BrowsableAttribute.

-vJ

Jon Skeet said:
Ah - you *can* do that, I believe, although I can't remember what the
attribute is. Presumably you can then override the existing property
(and maybe throws an exception) and apply the attribute to the
override.
 
public class A
{
public virtual String Name
{
get {etc}
set {set}
}
}

public class B : A
{
[Browseable(false)]
public override String Name
{
get {return base.Name;}
set {base.Name = value;}
}
}


I think it is [Browseable] anyway :-)


--
Pete
-------
http://www.DroopyEyes.com
Audio compression components, DIB Controls, FastStrings

http://www.HowToDoThings.com
Read or write articles on just about anything
 
Using the following attributes...

[Browsable(false), EditorBrowsable(EditorBrowsableState.Never,
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

.... along with the NotImplementedException should give you what you want.


----- Peter Morris [Droopy eyes software] wrote: -----

public class A
{
public virtual String Name
{
get {etc}
set {set}
}
}

public class B : A
{
[Browseable(false)]
public override String Name
{
get {return base.Name;}
set {base.Name = value;}
}
}


I think it is [Browseable] anyway :-)


--
Pete
-------
http://www.DroopyEyes.com
Audio compression components, DIB Controls, FastStrings

http://www.HowToDoThings.com
Read or write articles on just about anything
 
Back
Top