Results 1 to 13 of 13

Thread: how to turn off a public member in a derived class

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Posts
    115

    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.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    make it Protected instead of Public in the base class
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Posts
    115
    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 ?

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    protected will still let class b call the function in class a...it just wont be visible to anyone instancing class b.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Posts
    115
    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:

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    hmmm
    try Protected Friend
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    nevermind that wont work either.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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).

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    what about including NotInheritable Keyword in your A Class.I've not tried it but just a thought crossed my mind

  10. #10
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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

  11. #11
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261
    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.

  12. #12
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width