Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 9th, 2004, 11:59 AM
Authorized User
 
Join Date: Jan 2004
Location: , , .
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

//##



Reply With Quote
  #2 (permalink)  
Old February 9th, 2004, 12:10 PM
Friend of Wrox
 
Join Date: Aug 2003
Location: , , .
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
Reply With Quote
  #3 (permalink)  
Old February 9th, 2004, 12:14 PM
Authorized User
 
Join Date: Jan 2004
Location: , , .
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?

Reply With Quote
  #4 (permalink)  
Old February 9th, 2004, 12:16 PM
Friend of Wrox
 
Join Date: Aug 2003
Location: , , .
Posts: 205
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Have you tried getInstance?
Reply With Quote
  #5 (permalink)  
Old February 9th, 2004, 12:17 PM
Authorized User
 
Join Date: Jan 2004
Location: , , .
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No. How do I use that?

Reply With Quote
  #6 (permalink)  
Old February 9th, 2004, 12:27 PM
Friend of Wrox
 
Join Date: Aug 2003
Location: , , .
Posts: 205
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry I meant GetObject (http://msdn.microsoft.com/library/de...tgetobject.asp)
Reply With Quote
  #7 (permalink)  
Old February 9th, 2004, 01:00 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
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
Reply With Quote
  #8 (permalink)  
Old February 9th, 2004, 03:31 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Alameda, ca, USA.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:19 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.