Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 9th, 2004, 11:59 AM
Authorized User
 
Join Date: Jan 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I refer to a class instance in VB?

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

//##



 
Old February 9th, 2004, 12:10 PM
Friend of Wrox
 
Join Date: Aug 2003
Posts: 205
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old February 9th, 2004, 12:14 PM
Authorized User
 
Join Date: Jan 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

 
Old February 9th, 2004, 12:16 PM
Friend of Wrox
 
Join Date: Aug 2003
Posts: 205
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Have you tried getInstance?
 
Old February 9th, 2004, 12:17 PM
Authorized User
 
Join Date: Jan 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No. How do I use that?

 
Old February 9th, 2004, 12:27 PM
Friend of Wrox
 
Join Date: Aug 2003
Posts: 205
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry I meant GetObject (http://msdn.microsoft.com/library/de...tgetobject.asp)
 
Old February 9th, 2004, 01:00 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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
 
Old February 9th, 2004, 03:31 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
What instance? - Data-binding to my class stevefal Pro Visual Basic 2005 0 April 15th, 2008 06:02 PM
static field with class instance prakash.suthar Java Basics 2 July 3rd, 2006 01:41 AM
Instance of same class in main method ajit Java Basics 1 July 1st, 2006 06:26 PM
who destroy static variables(no instance of class) MikoMax J2EE 1 March 31st, 2004 08:01 AM
How to create a class instance dynamic??? ffbalota C# 0 September 21st, 2003 04:49 PM





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