variable-scope

Is variable updated "by reference"?

Hi all, I have the following simple script. <script> SPC = { a : [10], b : 10, t: function() { y = this.a; z = this.b; y[0]++; z++; alert('this.a[0] = ' + this.a[0] + '\nthis.b = ' + this.b) } } SPC.t(); SPC.t(); </script> Running it in your browser will display two alert ...

Can I include a function inside of another function (PHP)?

Is it possible to include one function inside another? To learn functions, I'm trying to create a combat sequence using PHP. The sequence would look like this: Dice would roll, assigning numbers to variables; Hero1 attack results are printed; Dice would roll, assigning numbers to variables; Hereo2 attack results are printed; Dice woul...

Problems with variable scope (JavaScript)

I have the following JavaScript (I'm using jQuery): function language(language) { var text = new Object(); $.ajax({ type: "GET", url: "includes/xml/languages/" + language + ".xml", dataType: "xml", success: function(xml){ $(xml).find('text').each(function(){ text[$(this).attr('id')] = $(this).te...

Using a class like a public variable in asp.net

I'm getting an error in .net when trying to declare a Public class on my code behind page. Partial Class _Default Inherits System.Web.UI.Page Public someVariable as integer Public someClass as className Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load [...] The error I'm ge...

Keeping variables global to the library scope in C

Is there any way to keep global variables visible only from inside a library while inaccessible from programs that access that library in C? It's not that it is vital to keep the variable protected, but I would rather it if programs couldn't import it as it is nothing of their business. I don't care about solutions involving macros. ...

Having 2 variables with the same name in a class that extends another class in Java

Hello everyone. Following is a part of my code for a project: public class Body extends Point{ public double x, y, mass; public Body() { x = y = mass = 0; } public Body(double x, double y, double mass) { this.mass = mass; this.x = x; this.y = y; } } public class Point { public double x; ...

C#: Limit global field's scope to method / property OR preserve method's / property's local variable's value between calls

Hi, I often have methods which are called regularly and have some "state" which has to be preserved between calls, as in: float lastTime = 0.0f; void Draw( float currentTime ) { if( currentTime - lastTime > 0.5f ) { // not enough FPS } lastTime = currentTime; } And it drives me nuts that the global "state" f...

Declaring a variable before initializing it in c++

Is it possible to declare a variable in c++ without instantiating it? I want to do something like this: Animal a; if( happyDay() ) a( "puppies" ); //constructor call else a( "toads" ); Basially, I just want to declare a outside of the conditional so it gets the right scope. Is there any way to do this without using pointers ...

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 it differ from my? ...

How to avoid setting variable in a try statement

My problem is that I have to set a variable in a try statement otherwise I get a compile error. Later on I need to use that variable but it is now out of scope, or so I believe. I initialise the variable outside the try statement and set it to null, I thought that it might then be accessible outside, but I still get a NullPointerExcepti...

Have you ever used a "class instance variable" in any of your Ruby code?

I can understand why you would need a class variable to keep track of things like the total number of objects that have been instantiated in that class. And I can understand why you would need an instance variable to store attributes of a particular object in that class. But class instance variables I just can't seem to justify. As I ...

Need help with callbacks and anonymous classes in Java

I am using some third party library to connect to a server via async protocol and get response back. For example method to get userid by username looks like this: public int getUserid(String username) { int userid = 0; connection.call("getUserid", new Responder() { public void onResult(final int result) { System.out.println(...

Why does assigning to my global variables not work in Python?

I'm having terrible trouble trying to understand python scoping rules. With the following script: a = 7 def printA(): print "Value of a is %d" % (a) def setA(value): a = value print "Inside setA, a is now %d" %(a) print "Before setA" printA() setA(42) print "After setA" printA() Gives the unexpected (to me) output of:...

Variable scope for PHP callback functions

Hi, In response to another question I asked about regular expressions, I was told to use the *preg_replace_callback* function (http://stackoverflow.com/questions/959017/) as a solution to my problem. This works great, but now I have a question relating to variable scope in callback functions. The function that parses the text is part o...

Have Firebug Break when a global variable X is defined

We have a very large JavaScript application where after many months of coding there have inevitably sprung a couple scope slip ups where a variable is defined in the following fashion: function() { x = 5; ... } instead of: function() { var x = 5; ... } This is happening somewhere -- We're not sure where -- and s...

Declaring variables and scope question for Lua

I'm lead dev for Bitfighter, and we're using Lua as a scripting language to allow players to program their own custom robot ships. In Lua, you need not declare variables, and all variables default to global scope, unless declared otherwise. This leads to some problems. Take the following snippet, for example: loc = bot:getLoc() item...

Ruby accessing outer variables in nested function

Hi, I'm sure there's a simple answer for this; I just can't seem to find it. I made a nested function in Ruby, and I was having trouble accessing variables from the outer function inside the inner function: def foo(x) def bar puts x end bar 42 end foo(5) I get: NameError: undefined local variable or method x' for main:Obj...

When should you use a package variable vs a lexical variable (and what's the difference)?

I'm looking at some older Perl code on Perl Monks to figure out programming with Win32::OLE and MS Word. Scattered throughout the code are variables with names like $MS::Word and the like, without a 'my' included in their declaration. After reading a bit on Google, I understand that these are called 'package variables' versus 'lexical va...

jQuery javascript scoping problem

I have a hidden input element that I am using as a counter to use to name more input elements generated by JavaScript. To get the value of the counter i use parseInt($('#counter').val()); However I use this code snippet several times in my code, so I thought it would be good to put it in a function function getCounter(){ parseInt...

How does variable scoping work in a POE session?

Can anyone explain how variable scoping works within a POE session? What is the proper way to pass state within the session, without impacting other sessions? Thanks Josh ...