+ Reply to Thread
Results 1 to 5 of 5

Lots of Global Variables. Is this a problem?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-13-2009
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    136

    Lots of Global Variables. Is this a problem?

    I keep reading here and there that global variables are bad. This program that I am writing has a lot going on and many of the variables are used across multiple modules. What are the problems with declaring these variables global?

  2. #2
    Registered User
    Join Date
    06-03-2009
    Location
    Tulsa, OK
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Lots of Global Variables. Is this a problem?

    The only problem I would see would be if you used a variable in a module and did not assign it a value in the beginning of the subroutine.
    Hope this helps.

  3. #3
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: Lots of Global Variables. Is this a problem?

    There's more to it than that: Romperstomper recommends passing values between routines and explains why global variables should be avoided wherever possible in this post:

    Quote Originally Posted by romperstomper View Post
    Mostly because they make debugging harder. Because they can be changed from anywhere at any time, it can be difficult to try and follow a logical program flow and see what's going wrong. They also violate the principle of encapsulation - i.e. your routines should be as self-contained as possible - and they have a nasty habit of getting reset if an unhandled error occurs in your code.
    Obviously they are not always avoidable, but a good rule of thumb is to give variables the narrowest scope you can.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Lots of Global Variables. Is this a problem?

    FWIW, I agree with RomperStomper.

    As a minimum, public variables should only be modified in one procedure.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Contributor
    Join Date
    03-13-2009
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: Lots of Global Variables. Is this a problem?

    I guess this works then. My program starts out with a form that asks the user to input various pieces of information. That information is assigned to the different global variables and then used in a number of different modules. None of the modules modify the variables, they only use the values in their calculations.
    Last edited by shg; 11-25-2009 at 12:32 AM. Reason: deleted spurious quote

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1