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. Class inheritance question

Class inheritance question

Scheduled Pinned Locked Moved C#
questionooptutorial
12 Posts 7 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.
  • M Offline
    M Offline
    Mc_Topaz
    wrote on last edited by
    #1

    Hi! Is it possible to have two instances of a classes which both instances share the same base class instance? For example:

    class Program
    {
        static void Main(string\[\] args)
        {
            Bar bar1 = new Bar();
            Bar bar2 = new Bar();
        }
    }
    
    class Foe
    {
    }
    
    class Bar : Foe
    {
    }
    

    Is this possible to get the instanees bar1 and bar2 to share the same base instance? /Mc_Topaz!

    M L S P M 6 Replies Last reply
    0
    • M Mc_Topaz

      Hi! Is it possible to have two instances of a classes which both instances share the same base class instance? For example:

      class Program
      {
          static void Main(string\[\] args)
          {
              Bar bar1 = new Bar();
              Bar bar2 = new Bar();
          }
      }
      
      class Foe
      {
      }
      
      class Bar : Foe
      {
      }
      

      Is this possible to get the instanees bar1 and bar2 to share the same base instance? /Mc_Topaz!

      M Offline
      M Offline
      Manas Bhardwaj
      wrote on last edited by
      #2

      Mc_Topaz wrote:

      Is this possible to get the instanees bar1 and bar2 to share the same base instance?

      What is your meaning with SHARE?

      Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

      1 Reply Last reply
      0
      • M Mc_Topaz

        Hi! Is it possible to have two instances of a classes which both instances share the same base class instance? For example:

        class Program
        {
            static void Main(string\[\] args)
            {
                Bar bar1 = new Bar();
                Bar bar2 = new Bar();
            }
        }
        
        class Foe
        {
        }
        
        class Bar : Foe
        {
        }
        

        Is this possible to get the instanees bar1 and bar2 to share the same base instance? /Mc_Topaz!

        L Offline
        L Offline
        lisan_al_ghaib
        wrote on last edited by
        #3

        huh sorry i dont understand

        1 Reply Last reply
        0
        • M Mc_Topaz

          Hi! Is it possible to have two instances of a classes which both instances share the same base class instance? For example:

          class Program
          {
              static void Main(string\[\] args)
              {
                  Bar bar1 = new Bar();
                  Bar bar2 = new Bar();
              }
          }
          
          class Foe
          {
          }
          
          class Bar : Foe
          {
          }
          

          Is this possible to get the instanees bar1 and bar2 to share the same base instance? /Mc_Topaz!

          S Offline
          S Offline
          Saranya B
          wrote on last edited by
          #4

          Hello... Your example code is pretty perfect and will work for sure. Still I dont understand what you are trying to ask. Cheers, Saran

          1 Reply Last reply
          0
          • M Mc_Topaz

            Hi! Is it possible to have two instances of a classes which both instances share the same base class instance? For example:

            class Program
            {
                static void Main(string\[\] args)
                {
                    Bar bar1 = new Bar();
                    Bar bar2 = new Bar();
                }
            }
            
            class Foe
            {
            }
            
            class Bar : Foe
            {
            }
            

            Is this possible to get the instanees bar1 and bar2 to share the same base instance? /Mc_Topaz!

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            I'm a bit unsure that you are asking here. Are you asking whether you can have two classes which are the same instance, i.e. using a Singleton pattern?

            Deja View - the feeling that you've seen this post before.

            My blog | My articles

            1 Reply Last reply
            0
            • M Mc_Topaz

              Hi! Is it possible to have two instances of a classes which both instances share the same base class instance? For example:

              class Program
              {
                  static void Main(string\[\] args)
                  {
                      Bar bar1 = new Bar();
                      Bar bar2 = new Bar();
                  }
              }
              
              class Foe
              {
              }
              
              class Bar : Foe
              {
              }
              

              Is this possible to get the instanees bar1 and bar2 to share the same base instance? /Mc_Topaz!

              M Offline
              M Offline
              Mc_Topaz
              wrote on last edited by
              #6

              Well, I shall try to explain what I mean by replying to my own post. When I create a instance for the Bar class, the code will automatically create an instance in the Foe class. So I have one Bar instance and also one Foe instance. Let's call the Bar class instance: bar1. Later I would like one more instance of the Bar class: bar2. But in this case, I don't want a brand new instance of the Foe class. Instead I want bar2 to have the same foe instance as bar1 have. Is this possible? I hope I explained it better this time.

              L C P 3 Replies Last reply
              0
              • M Mc_Topaz

                Well, I shall try to explain what I mean by replying to my own post. When I create a instance for the Bar class, the code will automatically create an instance in the Foe class. So I have one Bar instance and also one Foe instance. Let's call the Bar class instance: bar1. Later I would like one more instance of the Bar class: bar2. But in this case, I don't want a brand new instance of the Foe class. Instead I want bar2 to have the same foe instance as bar1 have. Is this possible? I hope I explained it better this time.

                L Offline
                L Offline
                lisan_al_ghaib
                wrote on last edited by
                #7

                Bar bar 1 = new Bar(); Bar bar2 = bar1;

                M 1 Reply Last reply
                0
                • M Mc_Topaz

                  Well, I shall try to explain what I mean by replying to my own post. When I create a instance for the Bar class, the code will automatically create an instance in the Foe class. So I have one Bar instance and also one Foe instance. Let's call the Bar class instance: bar1. Later I would like one more instance of the Bar class: bar2. But in this case, I don't want a brand new instance of the Foe class. Instead I want bar2 to have the same foe instance as bar1 have. Is this possible? I hope I explained it better this time.

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  No, that is not possible. Inheritance is the wrong technique for that. What you want to do is set up an association. Create a field in Bar that references the Foo object. You can then share the same Foo object between two Bar objects.

                  Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

                  1 Reply Last reply
                  0
                  • M Mc_Topaz

                    Well, I shall try to explain what I mean by replying to my own post. When I create a instance for the Bar class, the code will automatically create an instance in the Foe class. So I have one Bar instance and also one Foe instance. Let's call the Bar class instance: bar1. Later I would like one more instance of the Bar class: bar2. But in this case, I don't want a brand new instance of the Foe class. Instead I want bar2 to have the same foe instance as bar1 have. Is this possible? I hope I explained it better this time.

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    To extend what Colin said, take a look at using the Singleton pattern for the Foo class.

                    Deja View - the feeling that you've seen this post before.

                    My blog | My articles

                    1 Reply Last reply
                    0
                    • M Mc_Topaz

                      Hi! Is it possible to have two instances of a classes which both instances share the same base class instance? For example:

                      class Program
                      {
                          static void Main(string\[\] args)
                          {
                              Bar bar1 = new Bar();
                              Bar bar2 = new Bar();
                          }
                      }
                      
                      class Foe
                      {
                      }
                      
                      class Bar : Foe
                      {
                      }
                      

                      Is this possible to get the instanees bar1 and bar2 to share the same base instance? /Mc_Topaz!

                      realJSOPR Offline
                      realJSOPR Offline
                      realJSOP
                      wrote on last edited by
                      #10

                      You spelled "Foo" wrong.

                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                      P 1 Reply Last reply
                      0
                      • realJSOPR realJSOP

                        You spelled "Foo" wrong.

                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                        -----
                        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #11

                        I think he actually means it's Bars enemy.

                        Deja View - the feeling that you've seen this post before.

                        My blog | My articles

                        1 Reply Last reply
                        0
                        • L lisan_al_ghaib

                          Bar bar 1 = new Bar(); Bar bar2 = bar1;

                          M Offline
                          M Offline
                          Mc_Topaz
                          wrote on last edited by
                          #12

                          That worked! Thanks!

                          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