-
Hi,
Say, I have a class CPerson and Form1 (with 2 command buttons,
1 text box) in the project. The class has a FirstName property.
Code:
'Form1 code
Private Employee As CPerson
Sub cmdGetName_Click()
Set Employee = New CPerson
Employee.FirstName = Text1.Text
End Sub
Sub cmdShowName_Click()
MsgBox Employee.FirstName
End Sub
So first, I type a name in the Text1 textbox, I click cmdGetName, then I click cmdShowName. As I understand it, Employee object shouldn't exist by the time I click on cmdShowName because it should've been terminated upon exiting cmdGetName Sub. The object life stops when it's set to Nothing OR when it falls out of scope (out the procedure that was set in). I didn't set it to nothing but when cmdGetName goes through, it should automatically fall out of scope and stop existing. But it doesn't. The above program works. Does that mean that if I don't set object to nothing, it will exist between procedures?
Please help me understand this.
Thanks for help.
-
Anyone else ;) ?
Thanks again, it's very important.
-
Well ...
Try using the Set objRef = New Object statement in the form_load event.
-
Can anyone else explain why the object still exists?
Thanks
-
The set command does NOT define the scope of the object. The scope of the object is the same as the scope of the VARIABLE used to contain the object. In this case this is the form itself. Until you have Set the variable, the Object is equal to Nothing. Once you have set it, it has a value until you:
a) explicitly use Set Employee = Nothing
or
b) the form is closed and the Employee variable is destroyed.
You should clean up the object anyway in the Form closure events.
Try clicking cmdShowName BEFORE you click cmdGetName...
Hope that is clear.
Paul.