473,320 Members | 1,950 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,320 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 12316
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.