Results 1 to 5 of 5

Thread: Object's life question, please

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458
    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.
    Thanks

    Tomexx.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458
    Anyone else ?

    Thanks again, it's very important.
    Thanks

    Tomexx.

  3. #3
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Try using the Set objRef = New Object statement in the form_load event.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458
    Can anyone else explain why the object still exists?

    Thanks
    Thanks

    Tomexx.

  5. #5
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    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.
    Not nearly so tired now...

    Haven't been around much so be gentle...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width