473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Get class instance dynamically

6
Dear all,

I have a problem where I need to re-use the same instance of several classes over and over in my application. Like collections classes.

Is there a way, with reflection for example, of finding out at runtime if an instance of a class already exists and then using that instance?

Or do I have to create Singleton's for each of those classes?

Should I make a class that keeps track of all my other class instances? Is there maybe a pattern to do so?

Thanks in advance for any help.
Aug 21 '09 #1
7 2506
tlhintoq
3,525 Expert 2GB
If you only need ONE instance of a class, like a collection, you could make it STATIC and just use it with a variable that has global scope.
finding out at runtime if an instance of a class already exists
If it does exist didn't you have to assign it *to* something? Wouldn't you just check whatever variable you would have assigned that instance to?
Aug 21 '09 #2
tb2500
6
Ok on the first bit. Yes I could make it static. The problem is that I need this instance in several other different classes then where it was created.

So

Expand|Select|Wrap|Line Numbers
  1. Class A
  2. {
  3.      /// Some methods
  4. }
  5.  
  6. Class B
  7. {
  8. A = new A();
  9. a.DoSomething();
  10. }
  11.  
  12. Class C
  13. {
  14. // need same instance of A that B created... 
  15. }
You see what my problem is? An I have like 10 classes where I would need a particular instance of this one class?

@tlhintoq
Yes, but that variable is in another class as above.
Aug 21 '09 #3
Markus
6,050 Expert 4TB
Hmm, they'd have to be singletons, no? Also, one class creating a concrete instance of another class breaks encapsulation, doesn't it (unless the object is passed in as an argument)?
Aug 21 '09 #4
tb2500
6
I agree Markus, I think that I would need to implement the Factory pattern or some such to get around my problem. Unless there's another way?
Aug 21 '09 #5
GaryTexmo
1,501 Expert 1GB
What I took from what tlhintoq said was make a public class with a public static member that you can access...

Expand|Select|Wrap|Line Numbers
  1. public class GlobalContainer
  2. {
  3.   public static A MyA = new A();
  4. }
then...

Expand|Select|Wrap|Line Numbers
  1. Class B
  2. {
  3. //A = new A();
  4. //a.DoSomething();
  5. GlobalContainer.MyA.DoSomething();
  6. }
  7.  
  8. Class C
  9. {
  10. // need same instance of A that B created...
  11. GlobalContainer.MyA.<whatever>
  12. }
To be honest, I don't know what a singleton is... wikipedia seems to suggest that it's basically what I just wrote. I don't know what this has to do with "the Factory pattern" though...

Hopefully I've been of some help. Good luck!
Aug 21 '09 #6
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. Class B
  2. {
  3. A = new A();
  4. a.DoSomething();
  5. }
Shouldn't this be
Expand|Select|Wrap|Line Numbers
  1. A a = new A(); // so that a is a new instance of A so you can 
  2. a.DoSomething();
  3.  
Aug 21 '09 #7
tlhintoq
3,525 Expert 2GB
What about this? Make lists of your classes at program level so all can see.

Expand|Select|Wrap|Line Numbers
  1. Class Program()
  2. {
  3.     List<A> myAs = new List<A>;
  4.     List<B> myBs = new List<B>;
  5. }
Then add to those lists:
Expand|Select|Wrap|Line Numbers
  1. Class A
  2. {
  3.      /// Some methods
  4. }
  5.  
  6. Class B
  7. {
  8.    A newA = new A();
  9.    newA.CreatedByProperty = "B";
  10.    program.MyAs.add(newA);
  11.    newA.DoSomething();
  12. }
  13.  
  14. Class C
  15. {
  16. // need same instance of A that B created... 
  17.     program.MyAs[MyAs.Count-1]   {blah blah}
  18. // The most recently added 'A' is the last one in the list
  19. }
Now you have a List<> for each class that any other class can access.
You can add and search properties that let you know what type of class created it.
If you need to know any 'A' already just check the List<> MyA's.
If there is more than zero in the count, then an A exists.
If you need to know who made the existing 'A', check the CreatedBy property of each 'A' in the list
Aug 21 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Robert Oschler | last post by:
Hello, I am a Python newbie (by experience, not chronologically :) ), so if any of this doesn't make sense my apologies in advance. I am reading the chapter in The Python Cookbook on databases...
0
by: Peter | last post by:
I use dynamically loaded dll's a lot in my programs. The one thing i cannot figure out how do is too to sucessfully pass the address of a class instance created within the dynamically loaded dll. ...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
9
by: keith | last post by:
I created a class libery which has name space Assembly and class Assembly and compiled it. Then created a C# project and called a method in the external class e.g. Assembly dll;...
7
by: Buddy Ackerman | last post by:
I created this class Public Class HTMLFileInput : Inherits System.Web.UI.HtmlControls.HtmlInputFile Public Property Data As String Get Return ViewState("HTMLFileInput.Data") End Get Set...
9
by: sashang | last post by:
Hi I'd like to use metaclasses to dynamically generate a class based on a parameter to the objects init function. For example: class MetaThing(type): def __init__(cls, name, bases, dict,...
2
by: wilkinson.philip | last post by:
I have a javascript object which dynamically generates a table adding, deleting and moving rows as the user clicks on buttons or links. Problem is when I generate a table row and add the javascript...
6
by: jeff_j_dunlap | last post by:
Hello, Whenever I need class wide access to an object, I declare it dynamically: class myClass { ... myObject* obj; // declared dynamically ...
19
by: =?Utf-8?B?Sko=?= | last post by:
I have a logging component that I will access in other assemblies. So it was brought up to me that I should pass an instance around to these components instead of just making the logging class...
4
by: =?Utf-8?B?SmVzc2ljYQ==?= | last post by:
Hi All, I know that I can use the GetProcAddress to get the proc address of a global exported function from a WIn32 dll. But if I have an exported class in the dll, is there a way to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.