How to reference the loaded instance of Class A in Class B
-
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 .
-
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 .
The concept is called the "Singleton[^]" pattern.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
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 .
-
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 .
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?
-
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 .
Pass it to the constructor or a property.
B b = new B() ;
A a = new A ( b ) ;
or
a.B = b ;
-
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?
-
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[^]
-
It was more intended as a rhetorical question, like a "have you thought about this...?" 'sides, too busy hating on BizTalk to play around :(
-
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 .
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
-
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
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.