Questions tagged [scope]

Scope is an enclosing context where values and expressions are associated. Use this tag for questions about different types of scope as well for questions where scope may be unclear.

17524 questions
213
votes
4 answers

Static variables in member functions

Can someone please explain how static variables in member functions work in C++. Given the following class: class A { void foo() { static int i; i++; } } If I declare multiple instances of A, does calling foo() on one instance…
monofonik
  • 2,735
  • 3
  • 20
  • 17
204
votes
12 answers

What is the difference between 'my' and 'our' in Perl?

I know what my is in Perl. It defines a variable that exists only in the scope of the block in which it is defined. What does our do? How does our differ from my?
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
196
votes
8 answers

var self = this?

Using instance methods as callbacks for event handlers changes the scope of this from "My instance" to "Whatever just called the callback". So my code looks like this function MyObject() { this.doSomething = function() { ... } var self =…
defnull
  • 4,169
  • 3
  • 24
  • 23
186
votes
9 answers

Python nested functions variable scoping

I've read almost all the other questions about the topic, but my code still doesn't work. I think I'm missing something about python variable scope. Here is my code: PRICE_RANGES = { 64:(25, 0.35), 32:(13, 0.40), …
Stefan Manastirliu
  • 3,704
  • 3
  • 24
  • 18
185
votes
6 answers

What is the default scope of a method in Java?

If I type: void doThis(){ System.out.println("Hello Stackoverflow."); } what is the default scope of doThis()? Public? Protected? Private?
Joe Fontana
  • 2,306
  • 3
  • 18
  • 18
183
votes
1 answer

Angular 2 - Using 'this' inside setTimeout

I have a function like so in my class showMessageSuccess(){ var that = this; this.messageSuccess = true; setTimeout(function(){ that.messageSuccess = false; },3000); } How can I re-write this so I don't have to store a…
user2085143
  • 4,162
  • 7
  • 39
  • 68
181
votes
3 answers

Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?

Note: This is a reference question for dealing with variable scope in PHP. Please close any of the many questions fitting this pattern as a duplicate of this one. What is "variable scope" in PHP? Are variables from one .php file accessible in…
deceze
  • 510,633
  • 85
  • 743
  • 889
179
votes
9 answers

Spring Java Config: how do you create a prototype-scoped @Bean with runtime arguments?

Using Spring's Java Config, I need to acquire/instantiate a prototype-scoped bean with constructor arguments that are only obtainable at runtime. Consider the following code example (simplified for brevity): @Autowired private ApplicationContext…
Les Hazlewood
  • 18,480
  • 13
  • 68
  • 76
175
votes
16 answers

How to access outer class from an inner class?

I have a situation like so... class Outer(object): def some_method(self): # do something class Inner(object): def __init__(self): self.Outer.some_method() # <-- this is the line in question How can I access…
T. Stone
  • 19,209
  • 15
  • 69
  • 97
173
votes
5 answers

Declaring an enum within a class

In the following code snippet, the Color enum is declared within the Car class in order to limit the scope of the enum and to try not to "pollute" the global namespace. class Car { public: enum Color { RED, BLUE, WHITE …
bporter
  • 3,572
  • 4
  • 24
  • 23
165
votes
28 answers

Why aren't variables declared in "try" in scope in "catch" or "finally"?

In C# and in Java (and possibly other languages as well), variables declared in a "try" block are not in scope in the corresponding "catch" or "finally" blocks. For example, the following code does not compile: try { String s = "test"; // (more…
Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
157
votes
5 answers

Making code internal but available for unit testing from other projects

We put all of our unit tests in their own projects. We find that we have to make certain classes public instead of internal just for the unit tests. Is there anyway to avoid having to do this. What are the memory implication by making classes…
leora
  • 188,729
  • 360
  • 878
  • 1,366
156
votes
9 answers

How do I declare a global variable in VBA?

I wrote the following code: Function find_results_idle() Public iRaw As Integer Public iColumn As Integer iRaw = 1 iColumn = 1 And I get the error message: "invalid attribute in Sub or Function" Do you know what I did wrong? I…
Nimrod
  • 2,343
  • 3
  • 17
  • 7
155
votes
9 answers

Global variables in Javascript across multiple files

A bunch of my JavaScript code is in an external file called helpers.js. Inside the HTML that calls this JavaScript code I find myself in need of knowing if a certain function from helpers.js has been called. I have attempted to create a global…
Goro
  • 9,919
  • 22
  • 74
  • 108
151
votes
7 answers

Limiting number of displayed results when using ngRepeat

I find the AngularJS tutorials hard to understand; this one is walking me through building an app that displays phones. I’m on step 5 and I thought as an experiment I’d try to allow users to specify how many they’d like to be shown. The view looks…
Captain Stack
  • 3,572
  • 5
  • 31
  • 56