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. How to reference the loaded instance of Class A in Class B

How to reference the loaded instance of Class A in Class B

Scheduled Pinned Locked Moved C#
helptutorialquestion
10 Posts 6 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.
  • U Offline
    U Offline
    User 7763270
    wrote on last edited by
    #1

    Hi, I have two classes say Class A and Class B. I am trying to find how do I get reference the loaded instance of Class A in Class B. I do not want to get a new instance of Class A. I want to be able to reference to the loaded instance and use the loaded methods and properties. Class A { ... Class A myClass = New Class A(); ... ... Class B clsb = New Class B(); clsb.DoSomething(int a, int b) } Class B { ... public void DoSomething(int a, int b) { .... //I want to be able to reference to the loaded instance which is 'myClass' of the Class A. (not a new instance) } } Can some one please help me with this. Thanks, L .

    D D G P N 5 Replies Last reply
    0
    • U User 7763270

      Hi, I have two classes say Class A and Class B. I am trying to find how do I get reference the loaded instance of Class A in Class B. I do not want to get a new instance of Class A. I want to be able to reference to the loaded instance and use the loaded methods and properties. Class A { ... Class A myClass = New Class A(); ... ... Class B clsb = New Class B(); clsb.DoSomething(int a, int b) } Class B { ... public void DoSomething(int a, int b) { .... //I want to be able to reference to the loaded instance which is 'myClass' of the Class A. (not a new instance) } } Can some one please help me with this. Thanks, L .

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      The concept is called the "Singleton[^]" pattern.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      1 Reply Last reply
      0
      • U User 7763270

        Hi, I have two classes say Class A and Class B. I am trying to find how do I get reference the loaded instance of Class A in Class B. I do not want to get a new instance of Class A. I want to be able to reference to the loaded instance and use the loaded methods and properties. Class A { ... Class A myClass = New Class A(); ... ... Class B clsb = New Class B(); clsb.DoSomething(int a, int b) } Class B { ... public void DoSomething(int a, int b) { .... //I want to be able to reference to the loaded instance which is 'myClass' of the Class A. (not a new instance) } } Can some one please help me with this. Thanks, L .

        D Offline
        D Offline
        dan sh
        wrote on last edited by
        #3

        ClassA should provide a static method or a static property which returns the instance created inside it. This method/property will help others access that instance.

        "Your code will never work, Luc's always will.", Richard MacCutchan[^]

        1 Reply Last reply
        0
        • U User 7763270

          Hi, I have two classes say Class A and Class B. I am trying to find how do I get reference the loaded instance of Class A in Class B. I do not want to get a new instance of Class A. I want to be able to reference to the loaded instance and use the loaded methods and properties. Class A { ... Class A myClass = New Class A(); ... ... Class B clsb = New Class B(); clsb.DoSomething(int a, int b) } Class B { ... public void DoSomething(int a, int b) { .... //I want to be able to reference to the loaded instance which is 'myClass' of the Class A. (not a new instance) } } Can some one please help me with this. Thanks, L .

          G Offline
          G Offline
          GlobX
          wrote on last edited by
          #4

          If having a static reference (or the Singleton pattern, basically the same thing) is inappropriate, you could pass it in in the constructor or as a method parameter on class B. I'm surprised this works, though - maybe it's the pseudo-code you've presented, but this:

          public class ClassA()
          {
          private ClassA myClass = new ClassA();
          }

          is the same as:

          public class ClassA()
          {
          private ClassA myClass = null;

          public ClassA()
          {
              // infinite loop here ???
              myClass = new ClassA();
          }
          

          }

          Won't this end up with a stack overflow?

          D 1 Reply Last reply
          0
          • U User 7763270

            Hi, I have two classes say Class A and Class B. I am trying to find how do I get reference the loaded instance of Class A in Class B. I do not want to get a new instance of Class A. I want to be able to reference to the loaded instance and use the loaded methods and properties. Class A { ... Class A myClass = New Class A(); ... ... Class B clsb = New Class B(); clsb.DoSomething(int a, int b) } Class B { ... public void DoSomething(int a, int b) { .... //I want to be able to reference to the loaded instance which is 'myClass' of the Class A. (not a new instance) } } Can some one please help me with this. Thanks, L .

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

            Pass it to the constructor or a property.

            B b = new B() ;

            A a = new A ( b ) ;

            or

            a.B = b ;

            1 Reply Last reply
            0
            • G GlobX

              If having a static reference (or the Singleton pattern, basically the same thing) is inappropriate, you could pass it in in the constructor or as a method parameter on class B. I'm surprised this works, though - maybe it's the pseudo-code you've presented, but this:

              public class ClassA()
              {
              private ClassA myClass = new ClassA();
              }

              is the same as:

              public class ClassA()
              {
              private ClassA myClass = null;

              public ClassA()
              {
                  // infinite loop here ???
                  myClass = new ClassA();
              }
              

              }

              Won't this end up with a stack overflow?

              D Offline
              D Offline
              dan sh
              wrote on last edited by
              #6

              GlobX wrote:

              Won't this end up with a stack overflow?

              Why not just run it and see for yourself. I guess it will.

              "Your code will never work, Luc's always will.", Richard MacCutchan[^]

              G 1 Reply Last reply
              0
              • D dan sh

                GlobX wrote:

                Won't this end up with a stack overflow?

                Why not just run it and see for yourself. I guess it will.

                "Your code will never work, Luc's always will.", Richard MacCutchan[^]

                G Offline
                G Offline
                GlobX
                wrote on last edited by
                #7

                It was more intended as a rhetorical question, like a "have you thought about this...?" 'sides, too busy hating on BizTalk to play around :(

                D 1 Reply Last reply
                0
                • G GlobX

                  It was more intended as a rhetorical question, like a "have you thought about this...?" 'sides, too busy hating on BizTalk to play around :(

                  D Offline
                  D Offline
                  dan sh
                  wrote on last edited by
                  #8

                  GlobX wrote:

                  hating on BizTalk

                  Just work on SharePoint as well and you have witnessed hell. :)

                  "Your code will never work, Luc's always will.", Richard MacCutchan[^]

                  1 Reply Last reply
                  0
                  • U User 7763270

                    Hi, I have two classes say Class A and Class B. I am trying to find how do I get reference the loaded instance of Class A in Class B. I do not want to get a new instance of Class A. I want to be able to reference to the loaded instance and use the loaded methods and properties. Class A { ... Class A myClass = New Class A(); ... ... Class B clsb = New Class B(); clsb.DoSomething(int a, int b) } Class B { ... public void DoSomething(int a, int b) { .... //I want to be able to reference to the loaded instance which is 'myClass' of the Class A. (not a new instance) } } Can some one please help me with this. Thanks, L .

                    N Offline
                    N Offline
                    Nitheesh George
                    wrote on last edited by
                    #9

                    Hi, You need to use the Singleton pattern to achieve this. Please find the example below public class A { private static A _instance; private A() {} public static A GetInstance() { if(_instance == nulll) _instance = new A(); return _instance; } } public class B { public void Somemethod() { A a = A.GetInstance(); . . . do somethig . . } } Note that we make the constructor of class A private. And class A holds a instance of itself in _instance variable. This is for to make sure that an out side method cannot create an instance of A. Then we will provide a static method in class A like GetInstance where we create a new instance if an instance is already not exist and return the current instance Hope this helps Nitheesh George http://www.simpletools.co.in

                    P 1 Reply Last reply
                    0
                    • N Nitheesh George

                      Hi, You need to use the Singleton pattern to achieve this. Please find the example below public class A { private static A _instance; private A() {} public static A GetInstance() { if(_instance == nulll) _instance = new A(); return _instance; } } public class B { public void Somemethod() { A a = A.GetInstance(); . . . do somethig . . } } Note that we make the constructor of class A private. And class A holds a instance of itself in _instance variable. This is for to make sure that an out side method cannot create an instance of A. Then we will provide a static method in class A like GetInstance where we create a new instance if an instance is already not exist and return the current instance Hope this helps Nitheesh George http://www.simpletools.co.in

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

                      No, never a singleton, especially with .net languages; they're not needed and generally a symptom of poor design. I have never found a situation that wuold benefit from a singleton.

                      Nitheesh George wrote:

                      cannot create an instance of A

                      In .net everyone has access to your privates. And give this[^] a read.

                      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