Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Can a base class access a derived class?

Can a base class access a derived class?

Scheduled Pinned Locked Moved C#
question
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eggie5
    wrote on last edited by
    #1

    class Parent
    {
    //how do I access value from here?
    }

    class Child: Parent
    {
    string value="asdf";
    }

    /\ |_ E X E GG

    I C 2 Replies Last reply
    0
    • E eggie5

      class Parent
      {
      //how do I access value from here?
      }

      class Child: Parent
      {
      string value="asdf";
      }

      /\ |_ E X E GG

      I Offline
      I Offline
      il_masacratore
      wrote on last edited by
      #2

      Define a protected property or method overridable by the derived class. Example class Parent { protected virtual string Value { get{;} } private string GetDerivedValue() { return Value; } } class Child: Parent { string value="asdf"; protected override Value { get{return value;} } }

      Visit my blog at http://dotnetforeveryone.blogspot.com/

      1 Reply Last reply
      0
      • E eggie5

        class Parent
        {
        //how do I access value from here?
        }

        class Child: Parent
        {
        string value="asdf";
        }

        /\ |_ E X E GG

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        To access the value there, you need to use the 'as' keyword, as in Parent p = this as Parent; If p != null, then the object was indeed of the parent type, and you can then use the p object to access public properties ( and, I assume, protected ones )

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        P 1 Reply Last reply
        0
        • C Christian Graus

          To access the value there, you need to use the 'as' keyword, as in Parent p = this as Parent; If p != null, then the object was indeed of the parent type, and you can then use the p object to access public properties ( and, I assume, protected ones )

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          But wouldn't that violate OOP principles? (The parent knowing too much about the child.) Wouldn't the "proper" way be for the parent to specify that the child must have a particular method?

          class Parent
          {
          protected abstract string Value{get;} ;

          public override string ToString()
          {
              return ( Value ) ;
          }
          

          }

          class Child : Parent
          {
          string value="asdf";

          protected override string Value
          {
              get { return ( this.value ) ; }
          }
          

          }

          (I hope that's correct.)

          C 1 Reply Last reply
          0
          • P PIEBALDconsult

            But wouldn't that violate OOP principles? (The parent knowing too much about the child.) Wouldn't the "proper" way be for the parent to specify that the child must have a particular method?

            class Parent
            {
            protected abstract string Value{get;} ;

            public override string ToString()
            {
                return ( Value ) ;
            }
            

            }

            class Child : Parent
            {
            string value="asdf";

            protected override string Value
            {
                get { return ( this.value ) ; }
            }
            

            }

            (I hope that's correct.)

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            yes, you're right. The 'proper' was is definately for the property to be defined in the base method. But, if that's not possible, and if there's no way around it, what I described is the way to achieve what was asked for. Another way would be to define the property through an external interface, which is implimented by the derived class, and which is used in the 'as' call in the base class.

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            P 1 Reply Last reply
            0
            • C Christian Graus

              yes, you're right. The 'proper' was is definately for the property to be defined in the base method. But, if that's not possible, and if there's no way around it, what I described is the way to achieve what was asked for. Another way would be to define the property through an external interface, which is implimented by the derived class, and which is used in the 'as' call in the base class.

              Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              I don't even think that would work, Parent doesn't declare value. And the further problem would be if a child declares value as some unexpected type or not at all. The base class must either include the declaration of value or specify an abstract method either directly or via an interface.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups