Global variables

Results 1 to 3 of 3

Thread: Global variables

  1. #1
    Join Date
    Dec 1969

    Default Global variables

    Can anyone tell me why books etc always encourage you NOT to use global variables. For exmaple I often use a javascript customer constructor function to keep details about a customer which I can access across the site through the use of hidden frames. I also use global variables on ASP pages(yeah they are only global to this page, but global none the less for this page). Yeah there is the memory issue and they could be overwritten elsewhere but if you code correctly this should not happen. Everytime I read something about variables, it goes on about restricting the use of global variables, but I use them a lot and never have any problems. <BR><BR>Bill or anyone got an opinion on this?<BR><BR>Thanks in advance

  2. #2
    Join Date
    Dec 1969

    Default RE: Global variables

    it&#039;s a resource thing, and a neat coding thing (as you mention).<BR><BR>global variables hang around for a long time. they&#039;re also always there, and easy to forget about. basically, if they work for you, go for it.<BR><BR>j<BR>

  3. #3
    Join Date
    Dec 1969

    Default No such thing in ASP...

    ...really, there aren&#039;t! Unless you count the so-called Application variable. (Which are really just key/value pairs in a Collection object--that is, in a hash table.)<BR><BR>And, of course, too much usage of Application variables *could* eat memory, couldn&#039;t it?<BR><BR>A variable that is global to a page really only has page scope. When the page goes away, so does the so-called global.<BR><BR>Perhaps the most insidious "variables" in ASP are the so-called Session variables (again, there&#039;s really only *one* variable per user...the Session object...and then the so-called variables are just key/value pairs in the Session.Contents collection...another hash table).<BR><BR>In a sense, these *are* global variables. They certainly are global to ASP as a whole. The fact that one session can&#039;t see another session&#039;s contents is an aspect of the coding of the Session object methods, not an inherent part of the object model.<BR><BR>Anyway...<BR><BR>Atrax is right: Global variables are, typically, allocated once and their memory is never reclaimed. So they *can* be a resource hog. But they *can* be managed. Session "variables" are a good example of this. Their memory is released when the session expires or is abandoned. But, again, that&#039;s because of the way the Session object is coded.<BR><BR>Ehhh... Rather than saying "don&#039;t use global variables" I would offer "don&#039;t use a global variable when you don&#039;t need to." But as for page scope variables in ASP? Use them all you want. They are no more global than would be a member variable of a class in Java or C++. [Think of an ASP page as being an *instance* of a C++ class...which it actually is, by the way!...and it begins to make more sense. *ALL* contents of the page are only part of one instantiation of the class. Delete that instantiation--which is done automatically when a page is completed--and all the contents are erased from memory.]<BR><BR>

Posting Permissions

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