Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To 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 11th, 2004, 03:35 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

James,
never create circular references in ActiveX objects. For example:

private syb Button_Click()
  set theEngine = nwe classEngine
  .. do something else here
end sub

because theEngine is a local variable, VB automatically sets it to nothing before exiting the method, but if you have a circular reference theEngine will be still there eating up memory. In worst cases, even your application will not die hitting the X--the main dialog will close, but the application is still there and you can see in in the task manager.

There are many way to avoid circular reference. In your case I see two easy solutions. First is to declare B withevents, let B sends an event when it changes the variable (passing the variable itself) and class A to catch the event . At the end the two variables in class A and B will be the same. This is coumbersome but it works. I prefer the intermediate class. Create a class C and put the shared variables in it. Declare class C in class A, and pass it to class B via a property:
set myClassB.VarC = myClassC
in this way both A and B uses the same variable because it is in the same class. This is how I design my components after I learned the problem the hard way.
Well, there are ways including creating a 'weak' reference of class A using CopyMemory... I highly discorage the use of it. It is always better to look for 'native' VB solutions.

Marco
 
Old February 12th, 2004, 01:34 PM
Authorized User
 
Join Date: Jan 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Marco,

Thanks for your reply, but I am sorry to say that it is rather hard to follow your explanation.

My main problem is that I do not know how a class that I create as a result of a call from my spreadsheet, can be communicated with by other classes in my app (DLL) - because I do not have a handle (name) on that instance of the class.

In my code, the variable "theEngine" is not a local variable to the subroutine. It is a private variable of class B. I am using it to get a way to talk back to the class (theEngine) that created it. But I don't know if this is good practice or not. Can't see how else it could be done.

Still can't see a solution to this. Please can someone help, and shed light on my questions about clean-up etc.?

Thanks.

 
Old February 12th, 2004, 09:40 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi James,
thinks get fuzzy here :) let's see if I understood correctly, maybe I am losing it.
You created a dll with two classes, a public A and a private B. Your spreadsheet creates an instance of class A, and class A creates an instance of class B, that needs to talk back to A.
How am I going so far?
Marco
 
Old February 16th, 2004, 06:07 AM
Authorized User
 
Join Date: Jan 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Marco,

Yes - that is correct. Thanks for clarifying it. Can you help me find a solution? I have a feeling that if I create a new class C for shared variables, I will still have the same problem.

James






Similar Threads
Thread Thread Starter Forum Replies Last Post
Using class reference getting error rojiin ASP.NET 2.0 Basics 2 July 6th, 2007 01:53 PM
Reference Cache or Application from a class pauliehaha ASP.NET 2.0 Professional 1 April 17th, 2007 09:51 AM
Passing by reference quiksilverhg Excel VBA 5 November 21st, 2005 03:49 AM
Reference a class within ocx shahpragnesh_p Pro VB Databases 1 August 28th, 2003 08:31 AM
Passing by reference jacob ASP.NET 1.0 and 1.1 Basics 1 July 12th, 2003 05:07 PM





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