Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class_initialize 1

Status
Not open for further replies.

krinid

Programmer
Jun 10, 2003
356
CA
When is Class_initialize (and _Terminate) run and not run?

It seems to me that for

dim A as ClassName
set A = New ClassName


_initialize and _terminate are invoked, but for

dim A as new ClassName

they aren't.

Is this a 'feature' or am I just missing something?
(btw: I looked in the docs and couldn't find anything under 'class' or 'initialize' or anything else relevant, so I'm running on 0 information above my own experience)

Also, are there are another differences between the dim/set combo and dim as new?
 
The Initialize and Terminate events occur when you create and destroy object. So the first one takes place for:
Set A = New ClassName
the second:
Set A = Nothing

Declaration:
Dim A As New ClassName
creates object implicitly, its instance is created when you first reference it, and at this moment Class_Initialize procedure runs.

dim A As ClassName
does not create an object, only declares type of A, so no Initialize or Terminate is executed.

combo
 
combo,
Thanks for the reply. The problem I'm experiencing is that _Initialize isn't being invoked when I use 'dim as new'.

Private Sub Tester()
Dim Q As New ClassName
Stop ' A

Dim P As ClassName
Set P = New ClassName
Stop ' B
End Sub

The _Initialize event for ClassName are simply MsgBox's displaying "Initialize". The results are as follows:

Stop A: no MsgBox appears--this is strange!
Stop B: MsgBox appears, as expected
 
Krinid,
Suppose object of ClassName type has "Value" property.
The declaration Dim A As New ClassName does not create "A" immediately, so there is no "Initialize" event here. It is only information to be ready to create it when necessary.
When somewhere later in the code we first refer to it by, say, setting its value: A.Value=1, it is the moment when "A" object is created and "Initialize" event raised.
"A" will not be not created when there is no further reference to it in the code, in that case "Initialize" event will not fire.

So, in the test macro:

Private Sub Tester()
Dim Q As New ClassName ' ready to create Q
Stop ' A ' Q not created, Initialize event not fired

Dim P As ClassName
Set P = New ClassName ' P created here, Initialize event fired
Stop ' B

Dim R As New ClassName ' ready to create R
...
R.Value=1 ' R created here, Initialize event fired here
Stop ' C
End Sub

combo
 
Combo,
You've cleared the matter up for me. Cheers & a star for a clear explanation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top