locked
Hide Protected Property from 2nd Child Class RRS feed

  • Question

  • Hello,

    I have a Abstract Class OfficeAddinBase with a readonly property OfficeApplication.  I only want this property visuable in the ExcelAddinBase which inherits the OfficeAddinBase class.  When i create a class say lets say Class1 that inherits ExcelAddinBase i do not want it to be able to see the OfficeApplication Property, only the ExcelAddinBase.

    Thanks


    Coding 4 God!
    Friday, January 21, 2011 9:22 PM

Answers

  • There is no way to do that (in C#).  If you make the property visible to your subclasses, it will be visible to all of them, not just a single one.

     

    This is by design - a class should not be designed in a way where it requires knowledge of other classes that will use it.

     

    That being said, if you MUST work around this, you can put these two types into a separate assembly.  Instead of making that property protected, you could mark it as internal.  If ExcelAddinBase and OfficeAddinBase are the only two classes in that assembly, they will be the only ones who can see that property.

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Reed Copsey, JrMVP Friday, January 21, 2011 11:32 PM
    • Marked as answer by Mio_Miao Monday, January 24, 2011 12:12 PM
    Friday, January 21, 2011 11:02 PM

All replies

  • There is no way to do that (in C#).  If you make the property visible to your subclasses, it will be visible to all of them, not just a single one.

     

    This is by design - a class should not be designed in a way where it requires knowledge of other classes that will use it.

     

    That being said, if you MUST work around this, you can put these two types into a separate assembly.  Instead of making that property protected, you could mark it as internal.  If ExcelAddinBase and OfficeAddinBase are the only two classes in that assembly, they will be the only ones who can see that property.

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Reed Copsey, JrMVP Friday, January 21, 2011 11:32 PM
    • Marked as answer by Mio_Miao Monday, January 24, 2011 12:12 PM
    Friday, January 21, 2011 11:02 PM
  • Thank you that works perfect.  I was creating the addinbase classes in a seperate assembly anyways so that works.


    Coding 4 God!
    Friday, January 21, 2011 11:27 PM