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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Inheritance

Inheritance

Scheduled Pinned Locked Moved C#
questionoophelp
3 Posts 2 Posters 1 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.
  • Z Offline
    Z Offline
    zapap
    wrote on last edited by
    #1

    I have a parent class, Base with 2 child classes, Child1, and Child2. Base has a public int iNo. I init Child1 from another class. So Child1 inits Base. Now, in Child1, I set Base.iNo = 10. Then i create the second child class, Child2 from Child1. Now, in Child2, when I call Base.iNo, i don't get 10. I know the problem is that the child classes works with 2 seperate instances of the Base class, but how can i resolve this. i want both child classes to inherit from the same Base instance. How do i do this.? Thank you There's someone in my head but it's not me - Pink Floyd gunigugu

    G 1 Reply Last reply
    0
    • Z zapap

      I have a parent class, Base with 2 child classes, Child1, and Child2. Base has a public int iNo. I init Child1 from another class. So Child1 inits Base. Now, in Child1, I set Base.iNo = 10. Then i create the second child class, Child2 from Child1. Now, in Child2, when I call Base.iNo, i don't get 10. I know the problem is that the child classes works with 2 seperate instances of the Base class, but how can i resolve this. i want both child classes to inherit from the same Base instance. How do i do this.? Thank you There's someone in my head but it's not me - Pink Floyd gunigugu

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You inherit a class, not an object. Child1 and Child2 only inherits the definition of Base, Child1 objects and Child2 objects still are separeate objects. It's the same way as if you have two separate Base objects, they each have there own set of members, although they come from the same class. If you want to share a member across objects, you have to declare it as static. --- b { font-weight: normal; }

      Z 1 Reply Last reply
      0
      • G Guffa

        You inherit a class, not an object. Child1 and Child2 only inherits the definition of Base, Child1 objects and Child2 objects still are separeate objects. It's the same way as if you have two separate Base objects, they each have there own set of members, although they come from the same class. If you want to share a member across objects, you have to declare it as static. --- b { font-weight: normal; }

        Z Offline
        Z Offline
        zapap
        wrote on last edited by
        #3

        Thank you very much, I never thought of static variables... There is someone in my head but it's not me - Pink Floyd

        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