p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Beginning VB 6 (http://p2p.wrox.com/forumdisplay.php?f=75)
-   -   How do I refer to a class instance in VB? (http://p2p.wrox.com/showthread.php?t=9131)

James Diamond February 9th, 2004 11:59 AM

How do I refer to a class instance in VB?

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?




Yehuda February 9th, 2004 12:10 PM

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.


James Diamond February 9th, 2004 12:14 PM

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?

Yehuda February 9th, 2004 12:16 PM

Have you tried getInstance?

James Diamond February 9th, 2004 12:17 PM

No. How do I use that?

Yehuda February 9th, 2004 12:27 PM

Sorry I meant GetObject (http://msdn.microsoft.com/library/de...tgetobject.asp)

gbianchi February 9th, 2004 01:00 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..


Gonzalo Bianchi

marcostraf February 9th, 2004 03:31 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.


All times are GMT -4. The time now is 03:32 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.