DelbertZZZ
01-10-2005, 07:36 PM
I have a class library named X and one named Y.
The Windows app creates 3 instances of X (X1, X2, X3)
These 3 classes will instantiate a single instance of Y, but I want Y to be available to all of the X classes without the X classes having to create a new instance of Y everytime I need one of my X classes to invoke methods of the Y class.
What do I need to do?
Am I even making this clear?
blindreaper666
01-11-2005, 06:35 AM
I have a class library named X and one named Y.
The Windows app creates 3 instances of X (X1, X2, X3)
These 3 classes will instantiate a single instance of Y, but I want Y to be available to all of the X classes without the X classes having to create a new instance of Y everytime I need one of my X classes to invoke methods of the Y class.
What do I need to do?
You can use Inherits y for your x class to make the x classes as y's so ithas the ame properties and methods but you can also put your x methods in the x class. If you don't want one of the methods from the y class in your x class, put Overrides [sub name] and put the code in that like exit sub or whatever. That is a fundamental of object oriented programming.
excaliber
01-11-2005, 02:32 PM
I believe that if you declare a Private Shared instance of the Y class inside of the X class, any (and all) X classes will have access to it.
DelbertZZZ
01-12-2005, 01:18 PM
Thanks for all the replies, but I may not really be clear with my example so I'll try again...Keep in mind I am not sure if this can even be done.
Windows Form creates an instance of X (Dim whatever as new X)
when this happens I need this instance of X to see if an existence of Y has already been created and if not create an instance of Y. If it has then use the already instantiated Y
In this example if X1 is the very first instance of X created then it will instantiate a Y object. If the Windows form instantiates an additional X (X2) then X2 will check to see if a Y exists. (which in this case it should because X1 has already instantiated it.) I want to do this so that all my X objects will just use the same existing Y objects methods and such.
I need a sort of pseudo CreateObject so that all X classes will use an already instantiated Y class if one has already been created
wayneph
01-12-2005, 01:45 PM
Did you look into the Shared keyword that excalibur told you about? That is the best way to do it.
MSDN: Shared Members (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconsharedmembers.asp)
In your constructor you can just do something like this:
If Y Is Nothing Then
'Instantiate Y here
End If