Questions tagged [global-variables]

Global variables are variables that are accessible from all elements of a software component.

Global variables are variables that are accessible from all elements of a software component.

Global variables are used to share information between elements of a component and to store a part of the component's state. They therfore:

  • create a hidden coupling between all the elements that use them;
  • are a source of accidental bugs and inconsistencies, when changes and side-effects are not sufficiently controlled.
8810 questions
53
votes
10 answers

Can global constants be declared in JavaScript?

If so, what is the syntax for such a declaration?
Giffyguy
  • 20,378
  • 34
  • 97
  • 168
52
votes
3 answers

How to access global js variable in AngularJS directive

First of all, I checked and I didn't find any article covering my question. How to access a pre-defined js global variable in angularJS built-in directive? For example, I define this variable in Then I write…
windweller
  • 2,365
  • 5
  • 33
  • 56
51
votes
2 answers

Global dictionaries don't need keyword global to modify them?

I wonder why I can change global dictionary without global keyword? Why it's mandatory for other types? Is there any logic behind this? E.g. code: #!/usr/bin/env python3 stringvar = "mod" dictvar = {'key1': 1, 'key2': 2} def foo(): …
Jovik
  • 4,046
  • 6
  • 24
  • 24
50
votes
5 answers

Is there a spec that the id of elements should be made global variable?

If I have a
in Chrome then in javascript I can do a.stuff() (it's like as if a is a global variable). However this does not work with FireFox - I will need to use document.getElementById('a'). What is the correct behaviour here?…
Name
  • 741
  • 1
  • 8
  • 8
50
votes
9 answers

ok , global variable is condemned, singleton is despised, what's the alternative?

For desktop application that is. This is just a general question that maybe only need general answers.
mhd
  • 4,561
  • 10
  • 37
  • 53
50
votes
1 answer

Global variables joke

What’s the best naming prefix for a global variable? // I saw this joke on the wall in my CS lab and, being fairly inexperienced in C++, didn't get it. Could someone explain it to me?
Mutantoe
  • 703
  • 8
  • 20
50
votes
5 answers

How to make a local variable (inside a function) global

I'm using functions so that my program won't be a mess but I don't know how to make a local variable into global.
user1396297
49
votes
4 answers

filedialog, tkinter and opening files

I'm working for the first time on coding a Browse button for a program in Python3. I've been searching the internet and this site, and even python standard library. I have found sample code and very superficial explanations of things, but I haven't…
Icsilk
  • 567
  • 1
  • 5
  • 9
49
votes
6 answers

Global variables in c#.net

How can I set a global variable in a C# web application? What I want to do is to set a variable on a page (master page maybe) and access this variable from any page. I want to use neither cache nor sessions. I think that I have to use global.asax.…
scatman
  • 14,109
  • 22
  • 70
  • 93
49
votes
4 answers

Function not changing global variable

my code is as follow: done = False def function(): for loop: code if not comply: done = True #let's say that the code enters this if-statement while done == False: function() For some reason when my code…
cYn
  • 3,291
  • 6
  • 25
  • 43
45
votes
1 answer

Global variable Python classes

What is the proper way to define a global variable that has class scope in python? Coming from a C/C++/Java background I assume that this is correct: class Shape: lolwut = None def __init__(self, default=0): self.lolwut = default; …
nobody
  • 451
  • 1
  • 4
  • 3
45
votes
10 answers

Examples of the perils of globals in R and Stata

In recent conversations with fellow students, I have been advocating for avoiding globals except to store constants. This is a sort of typical applied statistics-type program where everyone writes their own code and project sizes are on the small…
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
45
votes
7 answers

name 'times' is used prior to global declaration - But IT IS declared

I'm coding a small program to time and show, in a ordered fashion, my Rubik's cube solvings. But Python (3) keeps bothering me about times being used prior to global declaration. But what's strange is that IT IS declared, right on the beggining, as…
44
votes
3 answers

Is std::cout guaranteed to be initialized?

What I know about C++ is that the order of the constructions (and destructions) of global instances should not be assumed. While I'm writing code with a global instance which uses std::cout in the constructor & destructor, I got a…
Inbae Jeong
  • 4,053
  • 25
  • 38
44
votes
13 answers

Declare global variables in Visual Studio 2010 and VB.NET

How do I declare a global variable in Visual Basic? These variables need to be accessible from all the Visual Basic forms. I know how to declare a public variable for a specific form, but how do I do this for all the forms in my project?
Tal
  • 457
  • 1
  • 4
  • 3