local-variables

Local variables with Delegates

This is clearly not appears like it wouldn't be a best practice, but can someone explain why or how this works. Or recommend a good book to learn more. //The constructor public Page_Index() { //create a local value string currentValue = "This is the FIRST value"; //use the local variable in a delegate that fires later ...

Programmatically get values of variables local to function in javascript?

Given: function foo(){ var bar = "quux"; console.log(/*mystery code here*/); } I'm looking for code that when inserted into the comment would yield the value of bar. By way of illustration, something like this works in a global scope: var foo = "bar"; var bar = "quux"; console.log(window[foo]); But, of course, variables defined...

Can someone explain these few lines of MSIL? Why does it move a value off the evaluation stack to a local variable, only to move it back immediately and return it?

The following MSIL code loads a single argument (a string), calls a method, which returns bool, and then returns that bool value. What I don't understand is why it calls stloc.0 to store the method's return value in a local variable, then performs an explicit unconditional control transfer to the very next labeled line (seems unnecessar...

Should you only use local variables in a partial?

Using local variables seems advisable in a partial that could be used application-wide to avoid dependencies across the application. But within a single controller it seems acceptable to reference instance variables that you know will be available in all of the actions that use the partial. If you do this, there seems to be a risk, h...

Rails partial locals not persisting when sent to another partial as its own local

I render a partial like so: <%= render :partial => 'widgets/some_partial, :locals => {:foo => 'bar'} %> So inside of _some_partial.html.erb I render two more partials like so: <% #foo.nil? #=> false %> <%= render :partial => 'widgets/another_partial', :locals => {:foo => foo} %> `<%= render :partial => 'widgets/another_partial_again'...

Dynamic MySQL with local variables

How can I use dynamic SQL statements in MySQL database and without using session variables? Right now I have such a code (in MySQL stored procedure): (...) DECLARE TableName VARCHAR(32); SET @SelectedId = NULL; SET @s := CONCAT("SELECT Id INTO @SelectedId FROM ", TableName, " WHERE param=val LIMIT 1"); PREPARE stmt FROM @s; EXECUTE stm...

Trouble with local variables in C#

I have a couple of variables that need to be assigned inside a for loop. Apparently, when the loop exits, C# ignores whatever happened in there, and the variables are returned to their original status. Specifically, I need them to be the last and next-to-last elements of a List. Here's the code: int temp1, temp2; for (int i = 0; i < toR...

Using variables in an array passed to a def - Rails

I'm using the Google charts API to generate a pie chart in my Rails application. However, I'm having a problem passing local variables to the def in the helper. The def takes a 2D array of [label, value] pairs. It doesn't like it when I try to pass a local variable in as the value. These are calculated ahead of time and are in currenc...

Where are Java final local variables stored?

Take the following example: public void init() { final Environment env = new Environment(); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { env.close(); } }); } Firstly, where is env stored? Is it: copied by the compiler into a hidden member variable of the inner clas...

How to declare a local constant in C# ?

How to declare a local constant in C# ? Like in Java, you can do the following : public void f(){ final int n = getNum(); // n declared constant } How to do the same in C# ? I tried with readonly and const but none seems to work. Any help would be greatly appreciated. Thanks. ...

Why do Variables that are local to a method cannot be declared final?

Hi I like to know why Variables that are local to a method cannot be declared final.. Is there any specific reason? Does it mean are there no local constants in java? ...

Releasing local variables before return?

In objective-c, I understand that you need to release anything you init/retain/copy. Do I need to do that before a return statement? I'm wanting to understand calling release explicitly and not use autorelease. -(void) someMethod { AnotherClass* ac = [[AnotherClass alloc] init]; if([ac somethingHappens]){ // Do I need to relea...

How to default-initialize local variables of built-in types in C++?

How do I default-initialize a local variable of primitive type in C++? For example if a have a typedef: typedef unsigned char boolean;//that's Microsoft RPC runtime typedef I'd like to change the following line: boolean variable = 0; //initialize to some value to ensure reproduceable behavior retrieveValue( &variable ); // do actual ...

Why are local variables also called "Automatic" in Java?

I read this in Kathy Sierra's book: "Local variables are sometimes called stack, temporary, automatic, or method variables, but the rules for these variables are the same regardless of what you call them." Why are the local variables called automatic? ...

In Applescript, why do local variables in handlers capture "with" labeled parameters?

In Applescript, if you declare a handler using "with" labeled parameters, local variables get the values of the arguments and the parameters themselves are undefined. For example: on bam of thing with frst and scnd local eat_frst return {thing: thing, frst:frst, scnd:scnd} -- this line throws an error end bam bam of "bug-AWWK!" ...

Using function arguments as local variables

Something like this (yes, this doesn't deal with some edge cases - that's not the point): int CountDigits(int num) { int count = 1; while (num >= 10) { count++; num /= 10; } return count; } What's your opinion about this? That is, using function arguments as local variables. Both are placed on the s...

MySQL SELECT, store in a variable

For a stored procedure, I want to do a SELECT, and store a column's value into a variable. How do I do this? I want to do something like this: DECLARE countTemp INT; SET countTemp=(SELECT COUNT(Name) FROM mytable WHERE Name= var_name LIMIT 0,1); OR, like this : DECLARE countTemp INT; SELECT countTemp=ColumnXYZ FROM ...

How to declare a variable in SQL Server and use it in the same Stored Procedure

Im trying to get the value from BrandID in one table and add it to another table. But I can't get it to work. Anybody know how to do it right? CREATE PROCEDURE AddBrand AS DECLARE @BrandName nvarchar(50), @CategoryID int, @BrandID int SELECT @BrandID = BrandID FROM tblBrand WHERE BrandName = @BrandName INSERT INTO tblBrandinCategor...

What's the scope of a Python variable declared in an if statement?

I'm new to Python, so this is probably a simple scoping question. The following code in a Python file (module) is confusing me slightly: if __name__ == '__main__': x = 1 print x In other languages I've worked in, this code would throw an exception, as the x variable is local to the if statement and should not exist outside of it....

Achieving variables in the local syntax in the Scheme environment

How we can achieve variables that we defined in the (local ...) syntax in Scheme? For example in this code below, (define (erkan x) (local ((define y 10)) (* x y))) how can I directly get the value of y ? ...