variable-scope

Question about Scope of Static Class Variables in Java

Hi Folks, I have a static object defined in my logging class, along the lines of: class myLoggingClass { static java.util.Properties properties; ... ... } According to my reference book, this means that the properties object is shared by all instances of my class. I find this definition insufficient. I'm writin...

Why do my controller's instance variables not work in views (Rails)

I would like to add a couple of instance variables to my controller, since the variables in question are required from within more than one action's view. However, the below example does not work as I would expect. class ExampleController < ApplicationController @var1 = "Cheese" @var2 = "Tomato" def show_pizza_topping # What ...

javascript closure immediate evaluation

Hi, Consider the following Javascript code: var a = []; var f = function() { for (var i = 0; i < 3; i++) { a.push(function(){alert(i)}); } for (var j = 0; j < 3; j++) { a[j](); } }; The alerts print out '3' all three times. I want a different behaviour - in each iteration of the loop generate a funct...

Preventing auto-creation of global variables in Javascript

I just spent some time debugging a problem that boiled down to forgetting to use the var keyword in front of a new variable identifier, so Javascript was automatically creating that variable in the global scope. Is there any way to prevent this, or change the default behavior, without using a validator like JSLint? Running a validator i...

Problem with using require inside a function and variable scope

I'm using an MVC setup and I'm trying to inject javascript into my views (.php), yet allow the javascript access to all the variables that the view has access to. My end goal is to be able to access PHP variables from my javascript (for example so I could alert() a product's name). Here's my application flow: start output buffer call ...

Flash Action Script 3 variable scope not working

package a{ public class a{ public var a_var:String; public var x_var:String; public function a(){ var a_var = 'My name'; var x_var = 'X string' } public function show_a():String{ return a_var; } } public class b{ public function b(){ var a_obj:a = new a(); trace('trace' ...

How can I increment a variable with value of another variable in JasperReports?

I need to make a grand total of the items I'm counting in a subReport. To do that, I think I need to add the value of that variable to another variable for each iteration, or "increment" it by that value. The subReport gets called for each group, and I get a total for that group. I need to add the variable values, rather than database co...

C#: Scope-specific variable binding

In some languages, there are things like these: Lisp: (let ((x 3)) (do-something-with x)) JavaScript: let (x = 3) { doSomethingWith(x); } Is there anything like this in C#? ...

AS3 - Updating Vars

Hello, I have a var to get thumbnails width, and it's set at the beggining of my class code. var thumbW:Number; Then I update that var inside a function that updades with stage resize. function x(){ var thumbW:thumbnails.width; //tracing thumbW here returns the updated value. Perfect! } Then I try to get the thumbW valu...

Effects of variable scope on performance? (C#)

Assume that we have 3 classes: Place, Contact, PhoneNumber classes. Under the Place class, I want to have a Contact class but it is better to keep PhoneNumber class under the Contact class. So in the Place class, it is more logical to reach the PhoneNumber by first getting the Contact object, then the PhoneNumber under the Contact obj...

Does Input/OutputStreams close on destruction?

Does InputStreams and OutputStreams in Java close() on destruction? I fully understand that this may be bad form (esp in C and C++ world), but I'm curious. Also, suppose I have the following code: private void foo() { final string file = "bar.txt"; Properties p = new Properties(); p.load( new FileInputStream(file) ); //...

How do you localize a number of legacy globals without eval?

I'm asking this question because I finally solved a problem that I have been trying to find a technique for in a number of cases. I think it's pretty neat so I'm doing a Q-and-A on this. See, if I could use eval, I would just do this: eval join( "\n" , map { my $v = $valcashe{$_}; sprintf( '$Text:...

Java: Accessing private fields directly from another instance of the same class

I'm writing a equals(Object obj) function for a class. I see that it is possible to access the private fields of obj from the caller. So instead of using a getter: Odp other = (Odp) obj; if (! other.getCollection().contains(ftw)) { } I can just access the field directly: Odp other = (Odp) obj; if (! other.collection.contains(ftw)) {...

Accessing parent variables in child method

Hi, I currently have two classes, one called Dog, one called Poodle. Now how can I use a variable defined in Dog from the Poodle class. My code is as follows: class dog { protected static $name = ''; function __construct($name) { $this->name = $name } } class Poodle extends dog { functi...

How can I Call Jquery Function From Other JavaScript function

How can I call the Jquery Function From other javaScript Function (Not from jquery function) i.e I have written some Jquery code like below $(document).ready(function() { function func1(){ // Do Something. } }); Now I want to call the func1() function from other JavaScript Function i.e Say an Example function...

How to pass a variable from one view controller to another?

Hi Everyone, I have three view controllers, one root controller, one login view controller and one customers view controller. I want to pass the entered username and password in login view controller to the customers view controller. My files and code is displayed below, could you please guide me, how can access to variables set in the ...

JavaScript Variable Scope

I'm having a problem with some JavaScript code. Script setTimeout(function() { for (var i = 0; i < 5; i++) { setTimeout(function() { console.log(i); }, i * 200); } }, 200); Outputs 5, 5, 5, 5, 5 instead of 1, 2, 3, 4, 5 I can kind of understand why this doesn't work, but I was wondering if someon...

Whats wrong with this code? creating a deleting variable in different scopes

I've recently seen some code similar to that outlined below. public void someMethod() { Lecture lect = createLecture(); ... lect.getLectureSeries().delete(); } public Lecture createLecture() { LectureSeries series = new Series(); Lecture lect = new Lecture(series); ... return lect; } The point being that some objec...

foreach, performance-wise. Should we declare variable once before loop or inside it?

What is better for performance wise declaring the variable outside the foreach statment and the each time reassign it in side it (foreach) or create an new variable inside foreach for example private List<ListItem> GetItems() { var items = new List<ListItem>(); var collection = new List<int> { 0, 1, 2, 3,...

Javascript/jQuery Variable Scope Issue

I have a jQuery script: $.ajax({ url: "/scripts/secure/development/ajax.asp", type: "POST", dataType: "text", data: "cmd=addresses", success: function(msg){ var arrRows = msg.split("#*#"); for(i=0;i<arrRows.length;i++){ var record_id = arrRows[i].split("|")[0]; var address = arrRows[i].split("|")[1]; ...