|
-
Jan 17th, 2003, 10:36 AM
#1
Thread Starter
Lively Member
how to turn off a public member in a derived class
Hi All,
I have two class A and B. A is a base class and B is derived from A. My question is how to make a public member in A invisible in B. For example,
Class A
public property p1() as string
Get
....
End Get
Set
......
End Set
End Class
Class B
inherits A
< put some code to turn off p1 >
End Class
for Class B, there is no B.p1. How can i do that ? Thanks.
-
Jan 17th, 2003, 10:44 AM
#2
make it Protected instead of Public in the base class
-
Jan 17th, 2003, 10:48 AM
#3
Thread Starter
Lively Member
Thanks Cander. You are right. But in my case, it has to be a public property in A so that it can be used by the functions dealing with class A. But for the functions dealing with class B, i want to hide the p1. Any suggestions ?
-
Jan 17th, 2003, 10:53 AM
#4
protected will still let class b call the function in class a...it just wont be visible to anyone instancing class b.
-
Jan 17th, 2003, 11:01 AM
#5
Thread Starter
Lively Member
Thanks again, Cander . Yes, i understand how protected works. But my problem is like below:
Dim clsA as ClassA
Dim clsB as ClassB
1: clsA.p1 = ...
2: clsB.p1= ...
i want to only 1: be legal operation not 2:
-
Jan 17th, 2003, 11:05 AM
#6
hmmm
try Protected Friend
-
Jan 17th, 2003, 11:06 AM
#7
nevermind that wont work either.
-
Jan 17th, 2003, 09:49 PM
#8
PowerPoster
Can you make the property overridable? Then in Class B, override it but make it private that way it isn't visible to the user and it would be illegal to use it.
That is theory, I can't run the test right now to see if that would work (don't have .Net on this machine).
-
Jan 18th, 2003, 11:01 AM
#9
Sleep mode
what about including NotInheritable Keyword in your A Class.I've not tried it but just a thought crossed my mind
-
Jan 18th, 2003, 11:33 AM
#10
Frenzied Member
But in my case, it has to be a public property in A so that it can be used by the functions dealing with class A. But for the functions dealing with class B, i want to hide the p1. Any suggestions ?
Make it private. The functions in class A will still have access to it.
Dont gain the world and lose your soul
-
Jan 18th, 2003, 12:17 PM
#11
Hyperactive Member
What about something like this:
Code:
public class MyClassA
Public Property Whatever
...
End Property
End Class
Public Class MyClassB
inherits MyClassA
Private Shadows Property Whatever
...
End Property
End Class
I havn't tried it yet, but I'm 99% sure it will work.
EDIT:
Never mind. It doesn't work (darn).
However, it just occured to me, that you really shouldn't be allowed to do this. Inheritance, by definition, allows derived classes to work exactly the same way as their base classes. If certain members have a different scope in derived classes, you completely lose that compatibility. (Example: I often pass a Bitmap object to the Graphics.drawImage procedure, eventhough drawImage accepts only the Image object. Since Bitmap is inherited from Image, I can often use them interchangably).
Last edited by Hu Flung Dung; Jan 18th, 2003 at 12:30 PM.
-
Jan 18th, 2003, 01:12 PM
#12
Sleep mode
Originally posted by DevGrp
Make it private. The functions in class A will still have access to it.
but that way he can't call the function from the class.
-
Jan 18th, 2003, 01:49 PM
#13
Hu Flung Dung is right. The OOP philosophy subset used in .Net doesn't allow this.
In C++ you could do it by deriving protected or private:
Code:
class B : protected A
{
};
but that isn't possible in the CLS.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|