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 April 23rd, 2004, 05:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dependant Child Classes

I have a VB Class Module (A) which has a function that returns another Class Module (B). This function can be called several times and each time the calling program receives a new instance of class B.

The problem I have is that if the calling program's instance of class A is destroyed then I want to be able to destroy all of the instances of class B that were created during the lifetime of the calling program's instance of class A. In other words I want to destroy all of the dependent children of class A.

How can this be done (can it be done) in VB?

Regards
Owain Williams
__________________
Regards
Owain Williams
 
Old April 27th, 2004, 04:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Owain, sorry I do not read this newsgroup that often and I miss this post of yours. Do you still have this problem?
Marco
 
Old April 28th, 2004, 04:06 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Marco, yes I am still perplexed as to how I can keep track of (and destroy) classes created by another class and returned to the calling program. Here is a very simple example of a class module I have tried:
Code:
Private ChildClass                  As Class1
Private intNumber                   As Integer

Public Property Let Number(ByVal NewNumber As Integer)
    intNumber = NewNumber
End Property

Public Property Get Number() As Integer
    Number = intNumber
End Property

Public Function GetChildClass(ByVal InitNumber As Integer) As Class1
    Set ChildClass = New Class1
    ChildClass.Number = InitNumber
    Set GetChildClass = ChildClass
End Function

Private Sub Class_Terminate()
    Set ChildClass = Nothing
End Sub
And here is an example of my calling program:
Code:
Sub Main()

    Dim ParentClass                 As Class1
    Dim ChildClass                  As Class1

    Set ParentClass = New Class1
    ParentClass.Number = 5
    Set ChildClass = ParentClass.GetChildClass(10)
    Debug.Print ParentClass.Number
    Debug.Print ChildClass.Number
    Set ParentClass = Nothing
    ' I would have expected the next line to cause an error
    ' because I have destroyed the parent class, the child
    ' class should have been destroyed as well.
    Debug.Print ChildClass.Number

End Sub
However the last line does print 10 into the immediate window and the child class has not been destroyed.

Regards
Owain Williams
 
Old April 28th, 2004, 04:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Owain,

When you execute this line:

Set ChildClass = ParentClass.GetChildClass(10)

what you are actually doing is creating 2 instances of ChildClass - one is private to ParentClass and the other is private to Sub Main. So when you execute Set ParentClass = Nothing then you are only destroying the instance that is private to ParentClass. The other instance still exists and that's the one that responds to the final line Debug.Print ChildClass.Number.

hth
Phil
 
Old April 28th, 2004, 07:20 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for that Phill, I guessed that that was what was happening. Referring back to my original question, how can I destroy the Sub Main's child class?

If you take for example the database object model, you create (among other things) a database connection object, then you create recordset objects based on the connection object. However if you destroy the database connection object, your recordset objects become invalid (because there is no longer a connection to the database they are based on).

How can I duplicate this behaviour in my own classes?

Regards
Owain Williams
 
Old April 28th, 2004, 07:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

You just Set ChildClass = Nothing in your Sub Main.

The other option is to not return the instance of ChildClass back to the calling program, but instead have it only held as a private variable in ParentClass.
 
Old April 28th, 2004, 09:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Obviously I can set the ChildClass in the calling program to Nothing, however I want this to happen automatically when the parent class is set to nothing. Is there no way that I can terminate all the classes that were created by a different class?
Code:
Set db = DBEngine.OpenDatabase("My Database.mdb")
Set rst = db.OpenRecordset("My Recordset")
Set db = Nothing
' The next line will raise an error because
' the connection has been closed.
rst.MoveLast
Is it possible to duplicate this behaviour in my own classes? e.g. the recordset class is created as a return value of a function in the database class. The database class is destroyed, the recordset class is no longer valid.

Regards
Owain Williams
 
Old April 28th, 2004, 02:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is what is happening.

Set ChildClass = ParentClass.GetChildClass(10)

In this line, ParentClass creates a new class and stores its reference into the local

Private ChildClass As Class1

And passes its handle to Main where it is stored in (Main):

    Dim ChildClass As Class1

So, in reality there is only one class, but with two handles, and therefore its referenceCount is 2 (remember that a VB class is a COM class)

When in the main you set

   Set ParentClass = Nothing

ParentClass 'tries' to destroy ChildClass in the Terminate:

    Set ChildClass = Nothing

but in fact what the above statement does is to decrease the class referenceCoount by 1. Because it was 2, it does not goes to zero (even at this age I can still make 1 plus 1, most of the time ;) therefore that class is not destroyed.

One question: why do you need a copy of ChildClass in childCLass? Oh, sorry, I guess that is a sample code, never mind that. Anyway, the trick is to avoid keeping unnecessary handles of the same class, otherwise it is hard to kill it. Even worse, a circular reference created in an ActiveX component will stop the application to close.

Let me know how it goes.
Marco





Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting Row Colours dependant on a field- FIXED Andrew.Berry ASP.NET 2.0 Basics 6 December 13th, 2007 05:59 AM
Dependant Dropdown Help phoenix211984 Dreamweaver (all versions) 0 October 11th, 2006 02:47 PM
Opening MDI Child on Button Click of MDI Child Sachin Shinde Visual Studio 2005 0 June 8th, 2006 05:05 AM
Classes digby_dog VB.NET 2002/2003 Basics 1 May 2nd, 2005 09:12 PM





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