Results 1 to 6 of 6

Thread: Questions on variables

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Question

    I have alot of global varaiables and arrays that I do not necessarily need to have active the length of the application.

    Upon exit, I have read to set all objects equal to nothing.

    What's the correct way of setting all variables equal to nothing? I've tried "set x = nothing", but VB expects an object? Is this necessary?

    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  2. #2
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Only concern yourself with Object Variables

    In your example, if x was declared as a String then it won't work. Just do nothing with these ones. If you have an object variable, which means you declared something like

    Code:
    Dim myObj as Collection
    Then you could/should (?) follow the principal of setting this to nothing once your program is finished with it.

    [code]
    Set myObj = Nothing
    [code]

    Note that to test if it is already Nothing, use:

    [code]
    If myObj is Nothing Then
    [code]

    With other variables, Stirngs and Variants in particular could be set to the equivalent to nothing. String can be set to "" and variants to "" or Null.

    Hope it helps
    Paul Lewis

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Cool Appreciate the help, but,

    What about arrays, integers, and bytes?
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    All non-object variables are deallocated when they go out of scope. That includes arrays, even of user-defined types.

    I think Objects are deallocated when you set them to Nothing, not when they go out of scope.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    More info

    I took from your original request that you wanted to know about how to let VB know you have finished with your global variables before the program ends.

    Generally this shouldn't be necessary because globals should be used for variables that are important throughout your entire app.

    If you ever do want to get rid of a global array during runtim I imagine you simply ReDim it to 0

    Code:
    'declare the array as being able to resize
    Dim BigArray() as String
    
    ...
    ' set the array size
    ReDim BigArray(2000)
    
    ...
    
    ' set the ne array size
    ReDim BigArray(0)
    I believe that VB will get around to garbage collecting the old array space so that the memory taken up by the elements will eventually return to the memory pool.

    As far as integers/bytes etc, these are not going to take up less space no matter what value you give them.

    I can find the MSDN documentation if you like. Also, it will be in almost any VB programmers guide if you want to know for sure all the "rules"

    Regards

    Paul Lewis

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Talking Thanks Paul, Thank all

    I appreciate all your help.

    Paul, the reson for my question was to ensure that the program releases all memory it can before closing.

    ie forms set to nothing

    Thank you for your reply.
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

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