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 10 12366
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 NotImplementExc eption();
}
}
even if you could make the MySubclass::DoS omething invisible it would still
be visible through it's super class
e.g.
MyBase mb = new MySubclass();
MyBase.DoSometh ing();
This is why you need to raise the NotImplementExc eption 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**********@R EMOVETHIShotmai l.com> wrote in message
news:7_Uec.6111 $ab3.1674@fed1r ead02... 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
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*******@code quest.co.uk> wrote in message
news:ur******** *****@tk2msftng p13.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 NotImplementExc eption(); } }
even if you could make the MySubclass::DoS omething invisible it would
still be visible through it's super class
e.g.
MyBase mb = new MySubclass(); MyBase.DoSometh ing();
This is why you need to raise the NotImplementExc eption 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**********@R EMOVETHIShotmai l.com> wrote in message news:7_Uec.6111 $ab3.1674@fed1r ead02... 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
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**********@R EMOVETHIShotmai l.com> wrote in message
news:7_Uec.6111 $ab3.1674@fed1r ead02... 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" <ja**********@R EMOVETHIShotmai l.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.
Jacob <ja**********@R EMOVETHIShotmai l.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 Substitutabilit y Principle -
basically, whatever you can do with a base class, you should be able to
do with a derived class.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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.co m> wrote in message
news:MP******** *************** @msnews.microso ft.com... Jacob <ja**********@R EMOVETHIShotmai l.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 Substitutabilit y Principle - basically, whatever you can do with a base class, you should be able to do with a derived class.
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Jacob <ja**********@R EMOVETHIShotmai l.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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
BrowsableAttrib ute.
-vJ
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... Jacob <ja**********@R EMOVETHIShotmai l.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.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 came up with (embedding in code, encrypting-decrypting)
involve embedding the/another secret in the code. since my problem cannot
request a user...
|
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 in reverse
order, calculates and prints their average.
=> We have been told that we need to make sure that our code meets
"information hiding"...
|
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 how can I make the
property hidden and not available at all?
Thanks,
Jacob
|
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 done so tht user doesnt come to know
the real source location of image file upon clicking its properties.
I've seen websites doing this. can somebody...
|
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 foo( char const ) {}
};
| |
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 to find out
if I've done anything wrong. Also, I am working on this for an
embedded environment, so if there are great inefficiencies with this...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |