Results 1 to 4 of 4

Thread: Lifetime of a public form variable

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    8

    Lifetime of a public form variable

    Hi all!

    From what I've read in manuals a variable declared as public in the Declarations section of a form only lives as long as this form is loaded.

    However, when I declare a public string variable (say, A) in a form (say, frm), initialise A, do some operations on A and then unload frm, I still can access A from other forms through frm.A which returns the value set at initialisation.

    Why is A not removed from the memory when frm is unloaded? I've tried searching the forum but couldn't find the answer.

    I use VB6.

    Thanks

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Lifetime of a public form variable

    I would suspect that you that you could possibly do something like:
    Code:
        Unload Form2
        Set Form2 = Nothing
    Whilst the variable contents will be destroyed, the reference to the (public) variable remains.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    8

    Re: Lifetime of a public form variable

    Thanks for the reply.

    I seem to have understood the sequence:

    The variable A is really removed from the memory when frm unloads, but any further reference to frm.A makes frm load again and execute Form_Load procedure, which sets A to the initial value.

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Lifetime of a public form variable

    Correct. The only way to totally "unload" the form is to unload the program itself.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

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