Consulting

Results 1 to 12 of 12

Thread: Solved: Global Variables vs Function Return values

  1. #1
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location

    Solved: Global Variables vs Function Return values

    I'm wondering if someone can let me know what the best method regarding global varilable usage vs Function Return values? Is there a reason to use one over the other? Is there a standard practice regarding both? Is one more effeicient then the other?
    I'm not talking declaring all varialbles as global, only ones that are used outside of the scope of the subroutine.
    I've done some searches and haven't found anything pretaining to this specfic topic, or with anyone who's judgement I would trust.

    Thanks in advance.

    Cal

    PS- I'm looking at this from an Excel VBA perspective, although I'm sure it will apply to VB and programing in general as well.
    The most difficult errors to resolve are the one's you know you didn't make.


  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Cal

    Any chance of some examples of what you mean?

    I've seen plenty of discussions over the years regarding this sort of think.

    I think a lot of people advocate not using Global/Public variables wherever possible.

    Obviously they have their uses under certain circumstances.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    My approach is to use global vbariables as little as is possible. This means that I will pass values to a function as parameters rather than have globals, and I will use ByRef to have updateable arguments, and return values from a function rather than use a global.

    I was always taught to restrict the scope of variables as much as you can. This means as few module level variables, as few globals as possible.

    As an aside, in my production code, I rarely use subs, invariably use functions.

  4. #4
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    Here's a couple of Examples.

    I've created a simple log class that creates a txt file and allows me to write to it. I usually define this a global varilable, so that I have access to it in each of my subroutines.
    I also created a global array to hold filenames in array's that I used in another subroutine.
    I think with the log it makes sense as a global array, but I figure the filearray should have been passed as a parameter to the other subroutine, then each filename passed as parameter to the next sub. I didn't do it that way, but thinking about it now that would have made more sense. I think from a readability standpoint for the code, it would be more understandable that way.
    The most difficult errors to resolve are the one's you know you didn't make.


  5. #5
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    That's the second time the board has posted stuff while I was typing it????? Just typing away and the next thing I know I got multiple posts? Not sure what key I managed to hit???
    The most difficult errors to resolve are the one's you know you didn't make.


  6. #6
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Cal,

    I deleted the 2nd one for you. Never had it happen to me personally, but I've noticed the odd duplicate by others here and there - maybe it's browser related thing?

    Regards,
    John

    EDIT: out of curiosity - I use IE6, what are you using?
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  7. #7
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    I'm using Flashpeak Slimbrowser, which supposedly uses the IE backbone with a suped up interface. I'm a touch typer, so I'm pretty sure I didn't hit and keys other then the ones I was supposed to? It's always a possiblity I hit some control combo I wasn't aware of.

    Cal
    The most difficult errors to resolve are the one's you know you didn't make.


  8. #8
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    I agree with Bob. I'll use them, but not very often. I like to keep things compartmentalized as much as possible. I pass a lot of variables between sub routines and use a lot of functions. There is the occasion where I'll need to store values in between the run-time of multiple routines. Public variables can come into play here, or I may find another way to store the data.

    I think the bottom line is there is no concrete answer. The less the better (IMO), but I don't know if there is a definite line of black and white, there is always some gray.

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by firefytr
    I agree with Bob. I'll use them, but not very often. I like to keep things compartmentalized as much as possible. I pass a lot of variables between sub routines and use a lot of functions. There is the occasion where I'll need to store values in between the run-time of multiple routines. Public variables can come into play here, ...
    ... or use classes

  10. #10
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    Thanks for the advice guy's. It seems to boil down to "Don't use global varailables unless you have no other choice, and there is always another choice"":-)

    Cal

    Umm..Ahh...Now if I can just figure out how to make this thread solved.
    :-)
    The most difficult errors to resolve are the one's you know you didn't make.


  11. #11
    VBAX Regular
    Joined
    Oct 2006
    Location
    Warsaw, Poland
    Posts
    23
    Location
    Hi,

    Quote Originally Posted by CBrine
    I'm wondering if someone can let me know what the best method regarding global varilable usage vs Function Return values? Is there a reason to use one over the other?
    VBA global variables in addition of simply being available to all other procedures have one additional feature: they remain in memory until you alter/reset VBA project.

    Even if macro finishes running the variable will still occupy the memory. In case of huge variables it may be easily visible in Task Manager / Processes (memory used by Excel.exe). In some cases it may be an advantage - keeping some data in global variable (much faster access, computations) while working with excel worksheet.
    One Hundred MS Excel Games

  12. #12
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by xld
    ... or use classes
    Yes! I love classes.

Posting Permissions

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