Wrox Programmer Forums
|
Pro VB 6 For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro 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 24th, 2004, 11:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jlick
Default

Well, it depends on how you need them to talk. You can pass an object into a method ByRef (passing a pointer), and as long as you don't store it in a module level variable, you don't really have a circular reference issue.

Basically VB is the same as working with COM in C++, with the exception that you control the reference counter in C++. VB handles the reference counting for you. The good news is that every reference is counted, and you have fewer details to track. The bad news is that you can't miscount purposefully.

Be careful if you go with the circular references, because you have to manage the cleanup well. I would try to keep the object structure as a tree, with references to the parent only. This way you can walk up & down the tree as needed, but you have clearly defined parent-child relationships for the purpose of top-down cleanup.

In 95% of the code I have done, I didn’t need circular references. In the other 5% I was always able to make a tree work (with a parent reference only). You can use the
Code:
With
block to make the code easier to read, and less typing. They also run faster if you are making multple calls to an object that is more than one jump.

Example: lets say you have the following relationship.
 A->B->C->D (D is a child of C; C is a child of B; ...)

If you need D to look at the data in A, you could do the following:

Following code would be in D
Code:
  With Me.Parent.Parent.Parent ' Me is D, first parent is C, etc.
    .Property1 = 5 ' Property in A
    .Property2 = 7 ' Property in A
    .Method1 ' Method in A
    '  .etc
  End With


John R Lick
[email protected]
 
Old February 25th, 2004, 06:53 AM
Authorized User
 
Join Date: Jan 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

John,

Yep - I think you are right. In my current application, I think that using this sort of tree structure is the most appropriate (making sure that I clean up systematically).

Thanks also for the coding tip.

Still happy to hear anyone else's views and experiences on this issue, if anyone wishes to contribute.

Cheers,

James

 
Old February 25th, 2004, 03:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear James,
it is not that crazy as you think, you just hit one of the VB limitations. But we hardcore programmers do not like to say "no I cannot do it"... I like John's idea of the tree structure, even though it is not always possible. I am using a clean up method in (only) one of my projects and I hate it. Also consider that sometimes just redesigning the class structure it is possible to reduce if not eliminate the chating between classes (I was able to do it and not only in VB).
But I know that sometimes it is not possible to eliminate the circular reefrence: subclassing is an example, even though we are not supposed to subclass in VB are we :) In that case I used the weak reference method, and I liked it. This is how it works (all the code in classB)

private m_prtClass as Long

public property let(myOBJ as Objevt)
  m_ptrClass = ObjPtr(myObj)
end property

m_ptrClass is used only to store the address of classA, without creating cicular reference. When ClassA is needed:

dim ca as ClassA
CopyMemory ca m_ptrClass, 4

At this point ca holds a valid pointer without having incremented the reference count. The only trick is to release it via:

CopyMemory ca, &0, 4

before ca goes out of scope, otherwise VB will decrement the reference count by one automatically.

Marco
 
Old March 3rd, 2004, 05:53 AM
Authorized User
 
Join Date: Jan 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Marco,

Thanks. It's an interesting issue.

Thanks for this suggestion, and all your previous input. It's very helpful.

Regards,

James






Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting VB6 classes to VB.NET (2003) jorgefejr VB How-To 2 August 25th, 2006 05:09 PM
talking to all txtBoxes via loop Loralee Access 11 October 7th, 2005 11:02 PM
talking to ms outlook from app wildt ASP.NET 1.x and 2.0 Application Design 0 March 24th, 2005 01:05 PM
Classes in VB6 - Urgent help required please James Diamond Pro VB 6 0 February 10th, 2004 11:42 AM
Call VB6 or VC++ classes from C# - Reflection? lpinho C# 0 September 22nd, 2003 09:44 AM





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