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. Inheritance instances

Inheritance instances

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

    Base ------------------------- | public int iNo; | | | ------------------------- Class1 : Base Class2 : Base ---------------------------- -------------------------- | Class2 c2; | | private int iMyNo; | | void Class1() | | public void SetMyNo()| | { iNo = 10; | | { iMyNo = iNo; | | c2 = new Class2(); | | } | | c2.SetMyNo(); | -------------------------- | } | ---------------------------- This is my srtucture, I begin with Class1, which then init Base, and then sets iNo = 10. Next I create a new instance of Class2, which in turn again init Base. Now when I call c2.SetMyNo, it sets is to nothing, since there are two instances of Base. So how do I inherit Class2 from the same instance of Base, the super class of Class1? There's someone in my head but it's not me - Pink Floyd gunigugu

    P 1 Reply Last reply
    0
    • Z zapap

      Base ------------------------- | public int iNo; | | | ------------------------- Class1 : Base Class2 : Base ---------------------------- -------------------------- | Class2 c2; | | private int iMyNo; | | void Class1() | | public void SetMyNo()| | { iNo = 10; | | { iMyNo = iNo; | | c2 = new Class2(); | | } | | c2.SetMyNo(); | -------------------------- | } | ---------------------------- This is my srtucture, I begin with Class1, which then init Base, and then sets iNo = 10. Next I create a new instance of Class2, which in turn again init Base. Now when I call c2.SetMyNo, it sets is to nothing, since there are two instances of Base. So how do I inherit Class2 from the same instance of Base, the super class of Class1? There's someone in my head but it's not me - Pink Floyd gunigugu

      P Offline
      P Offline
      Patric_J
      wrote on last edited by
      #2

      Your question is not very clear but if I understand it you have to two classes Class1 and Class2 both inheriting from Base. Then you create two objects, c1 from Class1 and c2 from Class2. Both objects will have its own copy of the iNo, inhereted from Base. They are not the same iNo int variable just because they are defined in the same class, instead they refer to seperate variables in memory. You don't want to use inheritance in this case, you want to use aggregation

      class Base
      {
      public int iNo;
      }

      class Class1
      {
      public Base myBase;
      }

      class Class2
      {
      public Base myBase;
      }

      class Test
      {
      [STAThread]
      static void Main()
      {
      Base b = new Base();
      b.iNo = 10;

          Class1 c1 = new Class1();
          c1.myBase = b;
      
          Class2 c2 = new Class2();
          c2.myBase = b;
      
          Console.WriteLine("c1.myBase.iNo = " + c1.myBase.iNo);
          Console.WriteLine("c2.myBase.iNo = " + c2.myBase.iNo);
          
          c2.myBase.iNo = 5;
      
          Console.WriteLine("c1.myBase.iNo = " + c1.myBase.iNo);
          Console.WriteLine("c2.myBase.iNo = " + c2.myBase.iNo);
      }
      

      }

      This will result in the following outpu c1.myBase.iNo = 10 c2.myBase.iNo = 10 c1.myBase.iNo = 5 c2.myBase.iNo = 5 /Patric My C# blog: C# Coach

      Z 1 Reply Last reply
      0
      • P Patric_J

        Your question is not very clear but if I understand it you have to two classes Class1 and Class2 both inheriting from Base. Then you create two objects, c1 from Class1 and c2 from Class2. Both objects will have its own copy of the iNo, inhereted from Base. They are not the same iNo int variable just because they are defined in the same class, instead they refer to seperate variables in memory. You don't want to use inheritance in this case, you want to use aggregation

        class Base
        {
        public int iNo;
        }

        class Class1
        {
        public Base myBase;
        }

        class Class2
        {
        public Base myBase;
        }

        class Test
        {
        [STAThread]
        static void Main()
        {
        Base b = new Base();
        b.iNo = 10;

            Class1 c1 = new Class1();
            c1.myBase = b;
        
            Class2 c2 = new Class2();
            c2.myBase = b;
        
            Console.WriteLine("c1.myBase.iNo = " + c1.myBase.iNo);
            Console.WriteLine("c2.myBase.iNo = " + c2.myBase.iNo);
            
            c2.myBase.iNo = 5;
        
            Console.WriteLine("c1.myBase.iNo = " + c1.myBase.iNo);
            Console.WriteLine("c2.myBase.iNo = " + c2.myBase.iNo);
        }
        

        }

        This will result in the following outpu c1.myBase.iNo = 10 c2.myBase.iNo = 10 c1.myBase.iNo = 5 c2.myBase.iNo = 5 /Patric My C# blog: C# Coach

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

        Thank you very much, however I think I'm going to use static variables. I'm quite new to programming and didn't know exactly how to work with static variables, so it didn't occurr to me. Regards 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