local-variables

Use of final local variables in java [duplicate]

陌路散爱 提交于 2019-11-28 22:36:50
This question already has an answer here: Why would one mark local variables and method parameters as “final” in Java? [closed] 12 answers I was wondering is there any usability of using final local variables. Variables are not overridden anyway when inheritance comes into picture. For example a simple code as below public static void main(String args[]) { final String data = "Hello World!"; System.out.println(data); } The example is quite simple one and may not be a relevant code but the question is more generic.I have seen a lot of codes(all incorporated in main function which have final

C# reusable function to dump current value of local variables

一个人想着一个人 提交于 2019-11-28 22:05:41
I would like to write a reusable function I can call within any method to log a snapshot of all the local variables. For example: void somemethod() { int a = 1; string s = "something"; dumpLocalVariables("step 1", MethodInfo.GetCurrentMethod(), this); a++; string t = s + "else"; dumpLocalVariables("step 2", MethodInfo.GetCurrentMethod(), this); } I would like to get a console output like this: step 1 Int32 a = 1 String s = something step 2 Int32 a = 2 String s = something String t = somethingelse I want to avoid providing a specific list of local variable names. The closest I could find was

angular - using async pipe on observable<Object> and bind it to local variable in html

筅森魡賤 提交于 2019-11-28 17:29:38
Hi I have a observable user$ with a lot of properties (name, title, address...) component{ user$:Observerable<User>; constructor(private userService:UserService){ this.user$ = this.userService.someMethodReturningObservable$() } } Is there a way to use the async pipe in the html template to subscribe to it and bind it to a local variable like this <div #user="user$ | async"> <h3> {{user.name}} </div> I know can can subscribe to it in the constructor and then unsubscribe in OnLeave/OnDestroy but I was just curious if I could use the async pipe. Cheers # is template reference variable . It defers

jQuery: how to access outside variable?

假装没事ソ 提交于 2019-11-28 13:55:04
问题 I am in a situation that needs be solve with this way; need convert a local variable to a global variable . There is an example returning image's real width and height which i found these method from this answer.. Need to convert local varialbes pic_real_height and pic_real_width to global variables with returning their true values. Here is jsFiddle. CSS : img { width:0px; height:0px; }​ jQuery : console.log($('.imgCon img').height());//returns 0 var img = $('.imgCon img')[0]; // Get my img

How to make a variable inside a try/except block public?

不想你离开。 提交于 2019-11-28 10:41:57
How can I make a variable inside the try/except block public? import urllib.request try: url = "http://www.google.com" page = urllib.request.urlopen(url) text = page.read().decode('utf8') except (ValueError, RuntimeError, TypeError, NameError): print("Unable to process your request dude!!") print(text) This code returns an error NameError: name 'text' is not defined . How can I make the variable text available outside of the try/except block? try statements do not create a new scope, but text won't be set if the call to url lib.request.urlopen raises the exception. You probably want the print

In Ruby, is there no way to dynamically define a local variable in the current context? [duplicate]

旧巷老猫 提交于 2019-11-28 10:03:38
This question already has an answer here: How to dynamically create a local variable? 4 answers I'm wondering if there is a method which will allow me to dynamically define a previously undefined variable in the current context. For example: foo # => NameError: undefined method or local variable ... # Some method call which sets foo = 1 in the local context foo # => 1 Put another way, given that foo is undefined, I'm looking for any code that would let me define the local variable foo without using the foo variable (e.g. if I had some other variable bar whose value was :foo and I had to rely

Ruby variable assignment in a conditional “if” modifier

与世无争的帅哥 提交于 2019-11-28 09:32:52
问题 I have a question about how the Ruby interpreter assigns variables: I use this quite often: return foo if (foo = bar.some_method) where some_method returns an object or nil. However, when I try this: return foo if (true && (foo = bar.some_method)) I get: NameError: undefined local variable or method foo for main:Object. What is the difference in evaluation between the first and second lines that causes the second line to error? 回答1: Read it carefully : Another commonly confusing case is when

how refer to a local variable share same name of a global variable in C? [duplicate]

若如初见. 提交于 2019-11-28 09:18:08
This question already has an answer here: How to print value of global variable and local variable having same name? 4 answers for example #include<stdio.h> int foo = 100; int bar() { int foo; /* local foo = global foo, how to implemented? */ return 0; } int main() { int result = bar(); return 0; } I think in the function bar, calling foo directly will just get the global foo. How can I refer the local foo? I know in C++, there is this pointer. However, does C has something similar? Thanks a lot! No, by declaring foo in bar() , you have taken the global foo out of scope. Inside bar() when you

Why it says that “Cannot refer to a non-final variable i inside an inner class defined in a different method”? [duplicate]

谁说胖子不能爱 提交于 2019-11-28 06:33:21
This question already has an answer here: Cannot refer to a non-final variable inside an inner class defined in a different method 20 answers I have button click listener and in onCreate() method I have a local variable like onCreate() { super.onCreate(); int i = 10; Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { i++; } }); Why java asks for to make me final ? When the onCreate() method returns, your local variable will be cleaned up from the stack, so they won't exist anymore. But the anonymous

Returning reference to a local variable

陌路散爱 提交于 2019-11-28 05:50:33
问题 Why can this code run successfully in Code::block. The IDB just reports warning: "reference to local variable ‘tmp’ returned", but ouput the result "hello world" successfully. #include <iostream> #include<string> using namespace std; const string &getString(const string &s) { string tmp = s; return tmp; } int main() { string a; cout<<getString("hello world")<<endl; return 0; } 回答1: Maybe this link will help you. 回答2: Upon leaving a function, all local variables are destroyed. By returning a