local-variables

C# reusable function to dump current value of local variables

ぐ巨炮叔叔 提交于 2019-11-27 14:12:50
问题 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 =

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

故事扮演 提交于 2019-11-27 10:31:25
问题 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

Undefined local variable for hash in method ruby

岁酱吖の 提交于 2019-11-27 08:56:39
问题 for some reason I'm getting NameError: undefined local variable or method `states' for main:Object though states is clearly defined. What is going on here? In irb I added states in and accessed it fine using states[:CA] but when I put it in a method I got that error. states = { CA: 'California', FL: 'Florida', MI: 'Michigan', NY: 'New York', OR: 'Oregon', } states[:CO] = 'Colorado' states[:HI] = 'Hawaii' cities = { CA: ['Alameda', 'Apple Valley', 'Exeter'], FL: ['Exeter', 'Amelia Island',

Ruby local variable is undefined

风格不统一 提交于 2019-11-27 07:13:13
I have the following Ruby code: local_var = "Hello" def hello puts local_var end hello I get the following error: local_variables.rb:4:in 'hello': undefined local variable or method 'local_var' for main:Object (NameError) from local_variables.rb:7:in '<main>' I always thought that local variables are not accessible from outside of the block, function, closure, etc. But now I defined local variable in the file and try to get an access from the function INSIDE the same file. What's wrong with my understanding? In Ruby local variables only accessible in the scope that they are defined. Whenever

Inner class and local variables

試著忘記壹切 提交于 2019-11-27 05:16:22
Why do I need to declare a local variable as final if my Inner class defined within the method needs to use it ? Example : class MyOuter2 { private String x = "Outer2"; void doStuff() { final String y = "Hello World"; final class MyInner { String z = y; public void seeOuter() { System.out.println("Outer x is "+x); System.out.println("Local variable is "+y); MyInner mi = new MyInner(); mi.seeOuter(); } } } } Why the String y needs to be a final constant ? How does it impact ? The answer is the two are in different scopes. So that variable could change before the inner class accesses it. Making

Python global/local variables

依然范特西╮ 提交于 2019-11-27 04:52:50
Why does this code work: var = 0 def func(num): print num var = 1 if num != 0: func(num-1) func(10) but this one gives a "local variable 'var' referenced before assignment" error: var = 0 def func(num): print num var = var if num != 0: func(num-1) func(10) Because in the first code, you have created a local variable var and used its value, whereas in the 2nd code, you are using the local variable var , without defining it. So, if you want to make your 2nd function work, you need to declare : - global var in the function before using var . def func(num): print num var = 1 <-- # You create a

Why a variable defined global is undefined? [duplicate]

拟墨画扇 提交于 2019-11-27 04:25:16
This question already has an answer here: 'Hoisted' JavaScript Variables 3 answers Hi guys here I have a simple function and a global variable. Why is myname undefined and not the string "global" ? var myname = "global"; // global variable function func() { alert(myname); // "undefined" var myname = "local"; alert(myname); // "local" } func(); Is not possible to refer to a outer variable that is define outside the scope of that function? and in this a global variable... And how I can fix this so I don't get a undefined from a global variable? You have just stumbled on a js "feature" called

Undefine variable in Ruby

三世轮回 提交于 2019-11-27 04:22:52
问题 Let's say I'm using irb , and type a = 5 . How do I remove the definition of a so that typing a returns a NameError ? Some context: later I want to do this: context = Proc.new{}.binding context.eval 'a = 5' context.eval 'undef a' # though this doesn't work. 回答1: There are remove_class_variable, remove_instance_variable and remove_const methods but there is currently no equivalent for local variables. 回答2: You can avoid un-declaring the variable by reducing the scope in which the variable

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

喜欢而已 提交于 2019-11-27 03:29:49
问题 This question already has answers here : How to dynamically create a local variable? (4 answers) Closed 4 years ago . 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

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

吃可爱长大的小学妹 提交于 2019-11-27 03:02:23
问题 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? 回答1: try statements do not create a new scope, but