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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class_Initialize firing several times when the object isn't even bein instantiated yet 2

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
0
0
GB
Hi,

I am creating a new class, which is instantiated on a form as follows...

Code:
Option Compare Database
Option Explicit
Private oNP As New clsNarrowPanel

I.E.

It's a form global, which I thought only gets created when the form opens, but it seems the class Class_Initialize is firing several times, before the form it is declared on is even opened?

What am I missing here?

Thanks,
1DMF.


"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
OK, it seems to be with bound subform object frame even when the source isn't set yet.

Access is opening forms on a non-visible tab control that doesn't even have the form as the source set against the subform control?

I changed the global to just the declaration, moved the instantiation to form open and it still fired several times.

So I moved the instantiation to form_current and Bingo!

Now only when the tab control is used, the page with the bound subform object is loaded with the form and an actual record is loaded onto the form does the class object get instantiated.

However, now each time the record changes it will fire... which I don't want, it is a global for the form not each record?

I guess it's a 'nothing test if statement' so at least it's only instantiated the once!



"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
>I guess it's a 'nothing test if statement' so at least it's only instantiated the once!

Won't work, since Is Nothing will always be False ... (and will itself cause an Initialize event if the class happens to be uninitialized)
 
IMO it is pretty sloppy to declare and instantiate at the same time for just this reason especially in event driven coding. I always declare the object than instantiate right before I need it.
Forms open from the inside out so the subform loads first which may be the cause of the problem. Try the onload event which occurs after the on open. In any case do not see how this could fire more than once. Put a message box in the on open event to test if that is in fact happening. If not there is something more to it.

 
>>IMO it is pretty sloppy to declare and instantiate at the same time for just this reason

I assume you mean in a VBA world not OOP in general?

>> Try the onload event which occurs after the on open
Yup , currently playing with that event handler :)

>> Won't work, since Is Nothing will always be False ... (and will itself cause an Initialize event if the class happens to be uninitialized)

How so? If I declare an object of a data type without the new, it's nothing isn't it until set to a new instantiated object, can you elaborate please Mike.

>> Put a message box in the on open event to test if that is in fact happening. If not there is something more to it.

Dang, you are right MajP, had some other global module code using the declaration with 'new' so it was other instances of the object firing the initialise - d'oh.

So I was missing something.

<< heading to bathroom to look for flannel to wipe egg off face! [hammer]



"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
>Private oNP As New clsNarrowPanel

This form of declaration creates a self-instantiating class. Every time you try and access it checks to see if it is already instantiated and, if not, instantiates itself. You can get some very odd bugs if you are not aware of this behaviour.
 
Yes, but I meant remove the 'new' and then use 'nothing check', so it is only instantiated the once.

Got it working perfectly now, so appreciate the heads up from both!

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
@Strongm
won't work, since Is Nothing will always be False
Not sure what you are referring to but that is incorrect.
Simple test
Code:
dim rs as adodb.recordset
msgbox rs is nothing
set rs = new adodb.recordset
msgbox rs is nothing
true
false
Maybe you are referrring to isNothing used on value types?

@
1DMF
I assume you mean in a VBA world not OOP in general?
No. OOP in general. One of the tenants of good code design is always to limit the lifetime and scope of any variables. Why instantiate an object prior to being ready to use it. There may be reasons that you can declare and instantiate at the same time, but do not do it out of habit. Do it as the exception where it makes sense to do it. Your example shows just how there may be unattended consequences.
 
Nope. You are not using the form of declaration that 1DMF was using.

Private rs As New ADODB.Recordset
MsgBox rs Is Nothing

 
I see the confustion. You missed the part where he said he had changed the module level variable to only declaration and moved the instantiation to the open event.
I changed the global to just the declaration, moved the instantiation to form open and it still fired several times.
....
I guess it's a 'nothing test if statement' so at least it's only instantiated the once
 
Thanks MajP, think Mike needs to go to spec-savers [bigglasses]

TBH I do use a few 'declare as New' global form variable, because they are like model/orchestrating controllers for MVC and then use an UpdateView sub to populate unbound controls on the form.

I have some apps with a mish-mash of bound and unbound form controls and where possible I am moving to an unbound MVC type paradigm especially when I add new functionality.

I thought (and was taught by the OU), that having a global form controller class instantiated when the form opens and lasts the life-cycle of the form, was better than every form event handler keep creating and destroying multiple copies of the same object?



"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top