|
Subject:
|
How do I refer to a class instance in VB?
|
|
Posted By:
|
James Diamond
|
Post Date:
|
2/9/2004 10:59:04 AM
|
Hello...
Basic VB question here:
I have two classes in my application (it's actually an ActiveX DLL). Class A is instantiated by a spreadsheet that incorporates the DLL.
An instance of Class B is created by the instance of Class A.
Now: in the instance of Class B, I want to reference a variable belonging to the instance of class A.
How do I refer to this variable (let's say the variable's name is X)?
The first problem is that I don't even know how to reference the instance of Class A from the instance of Class B.
Am I making the right approach in the first place?
Please can you help?
Thanks!
James
//##
|
|
Reply By:
|
Yehuda
|
Reply Date:
|
2/9/2004 11:10:17 AM
|
I am a little lost with all the As and Bs, but I think you want to reference an instance of an object created outside a class from within the class (essentially when a class is instatiated it is much the same as any other object). If you need the reference throughout the class I suggest using a set Property. If it is only one fuction pass the object as a parameter.
Yehuda
|
|
Reply By:
|
James Diamond
|
Reply Date:
|
2/9/2004 11:14:05 AM
|
The problem here is that I don't have a handle of the instance of Class A, since it was created by a call from the spreadsheet. So... how do I refer to it, in order to get/set one of its variables?
|
|
Reply By:
|
Yehuda
|
Reply Date:
|
2/9/2004 11:16:04 AM
|
Have you tried getInstance?
|
|
Reply By:
|
James Diamond
|
Reply Date:
|
2/9/2004 11:17:02 AM
|
No. How do I use that?
|
|
Reply By:
|
Yehuda
|
Reply Date:
|
2/9/2004 11:27:16 AM
|
Sorry I meant GetObject (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctgetobject.asp)
|
|
Reply By:
|
gbianchi
|
Reply Date:
|
2/9/2004 12:00:57 PM
|
hi there.. a better aproach maybe???
program -> class a -> class b
and class b needs something from class a..
there are two ways to aproach this..
class b has a lot of public variables so class a can give class b what it needs..
when setting up class b, it has a public variable of type class a ;) that way you do set class b = new class b set classb.pointerclassa = me
the last idea i don't know if will work..
HTH...
Gonzalo Bianchi
|
|
Reply By:
|
marcostraf
|
Reply Date:
|
2/9/2004 2:31:02 PM
|
Hi James, please consider revising your code, because referencing a variable of a class of which you do not have the handle is a poor design. Today it is one variable, tomorrow they are two or three... If more than one class needs access to 'common' variable, better to put them in a separate class, and pass this class along. Besides fixing your code, your code will be much and much easier to maintein. This is how it goes. Design class C to keep the 'shared' variables (they can be Public members or private with Get/Let/Set to allow validation, read only, saving or restoring). Your app creates one instance of class C and pass it to class A. Class A creates class B and pass C to it and go on. Notice that in this way you do not create any circular reference. If the variables are shared only between A and B, then it will be A to instanciate C. There is also another solution (it all depends on your application). You can declare that variable only in class B and expose it, and class A (that has the handle to B) can call it from B. This is easier, but I prefer the intermediate class. It is more work and even I sometimes prefer to avoid it, but at the end I always realize that it was the better choice to start with.
Marco
|