473,385 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

"Hiding" an inherited property?

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
Nov 16 '05 #1
10 12330
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.

"Jacob" <ja**********@REMOVETHIShotmail.com> wrote in message
news:7_Uec.6111$ab3.1674@fed1read02...
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

Nov 16 '05 #2
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
"Gary Hunt" <be*******@codequest.co.uk> wrote in message
news:ur*************@tk2msftngp13.phx.gbl...
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.

"Jacob" <ja**********@REMOVETHIShotmail.com> wrote in message
news:7_Uec.6111$ab3.1674@fed1read02...
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


Nov 16 '05 #3
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

--
mauro servienti

..

"Jacob" <ja**********@REMOVETHIShotmail.com> wrote in message
news:7_Uec.6111$ab3.1674@fed1read02...
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

Nov 16 '05 #4
"Jacob" <ja**********@REMOVETHIShotmail.com> wrote:
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.
Nov 16 '05 #5
Jacob <ja**********@REMOVETHIShotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Mauro Servienti <ma************@online.nospam> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Jacob <ja**********@REMOVETHIShotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #8
Jacob <ja**********@REMOVETHIShotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
BrowsableAttribute.

-vJ

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jacob <ja**********@REMOVETHIShotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #10
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
Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Picho | last post by:
Hi all, Lets say I have a "secret" I wish to "hide", lets say a database password. For the more detailed problem, a web application/service that uses a connection string. all the solutions I...
5
by: Amir S. | last post by:
Hi, I'm a newbie to C++ (2 weeks into the course). We were given this assignment to write some code that reads a set of integers (grades) from a file (filename passed by console), outputs them...
10
by: Jacob | last post by:
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...
10
by: FX | last post by:
I wanna publish a script on my site which allows me to hide image source. i have rough idea abt it. i`ll point src to some php page like: <img src="image.php"> & in tht php wat exactly shud be...
3
by: Nicolas Castagne | last post by:
Hi all, I have been wondering for a while why function hiding (in a derived class) exists in C++, e.g. why when writing class Base { void foo( int ) {} }; class Derived: public Base { void...
11
by: sofeng | last post by:
I'm not sure if "data hiding" is the correct term, but I'm trying to emulate this object-oriented technique. I know C++ probably provides much more than my example, but I'd just like some feedback...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.