scope

Is it wrong to use braces for variable scope purposes?

允我心安 提交于 2019-12-19 05:08:05
问题 I sometimes use braces to isolate a block of code to avoid using by mistake a variable later. For example, when I put several SqlCommand s in the same method, I frequently copy-paste blocks of code, ending by mixing the names and executing twice some commands. Adding braces helps to avoid this situation, because using a wrong SqlCommand in a wrong place will result in an error. Here's an illustration: Collection<string> existingCategories = new Collection<string>(); // Here a beginning of a

javascript: onload and onerror called together

江枫思渺然 提交于 2019-12-19 04:03:13
问题 I'm new to JavaScript and therefore confused for the variable scope... I'm trying to load an image, and replace it with another URL when it doesn't exist. I have to do it in pure JavaScript. Here I got 2 versions extremely alike, but they perform differently. The only thing in common is: they don't work. The 3rd version requires a refresh and doesn't work under IE. d is the object with number attribute, which has no problem. Here is what they have in common .attr("xlink:href", function (d) {

Kotlin - Restrict extension method scope

余生长醉 提交于 2019-12-19 03:39:07
问题 Is there a way to restrict extension methods in DSLs? Say I have a class structure like this: class Outer { fun middle(op: Middle.() -> Unit): Middle { val middle = Middle() middle.op() return middle } } class Middle { fun inner(op: Inner.() -> Unit): Inner { val inner = Inner() inner.op() return inner } } class Inner fun outer(op: Outer.() -> Unit): Outer { val outer = Outer() outer.op() return outer } I can then create a call like so: outer { middle { inner { middle { } // Here is the

Understanding dependency injection in AngularJS controllers

白昼怎懂夜的黑 提交于 2019-12-19 03:11:57
问题 Just learning dependency injection, and I think I'm starting to understand it. Please tell me if I'm on the right track... E.g.: Are these two equivalent? /* injection method */ function <controller_name>($scope) {} <controller_name>.$inject = ['$scope']; /* other method */ var app = angular.module('myApp'); app.controller(<controller_name>, function($scope) {}); 回答1: First a little clarification: For dependency injection, it doesn't matter whether you declare a controller using a global

How do I dynamically create functions that are accessible in a parent scope?

怎甘沉沦 提交于 2019-12-19 02:27:20
问题 Here is an example: function ChildF() { #Creating new function dynamically $DynFEx = @" function DynF() { "Hello DynF" } "@ Invoke-Expression $DynFEx #Calling in ChildF scope Works DynF } ChildF #Calling in parent scope doesn't. It doesn't exist here DynF I was wondering whether you could define DynF in such a way that it is "visible" outside of ChildF. 回答1: The other solutions are better answers to the specific question. That said, it's good to learn the most general way to create global

Why do list operations in python operate outside of the function scope? [duplicate]

被刻印的时光 ゝ 提交于 2019-12-19 02:27:09
问题 This question already has answers here : How do I pass a variable by reference? (26 answers) Closed 5 years ago . In the python code below, variable number is passed to the function addone , and a local copy is operated on. The value of number stays the same. def addone(num): num = num + 1 print "function: added 1, now %d" % num number = 5 print "Before:", number addone(number) print "After:", number Output : Before: 5 function: added 1, now 6 After: 5 However, the behavior appears to be

php array_map callback parameter scope

☆樱花仙子☆ 提交于 2019-12-18 21:16:24
问题 In the following code the callback function passed to wrap_map can't see the argument in the outer function, why? (see code comment for detail) public static function wrap_implode($ar, $wrap, $delim){ echo "wrap is $wrap"; //wrap is ok $res = array_map(function($val){ echo "wrap is $wrap"; //wrap is not set here! return $wrap. $val . $wrap; }, $ar); return implode($delim, $res); } 回答1: Because it is in another scope. If you want to use $wrap , try: function($val) use ($wrap){ //etc } Of

static keyword useless in namespace scope?

蓝咒 提交于 2019-12-18 18:40:00
问题 namespace N { static int x = 5; } What could be the importance/use-cases of declaring having a static variable at namespace scope? 回答1: Annex D (Compatibility features) [C++03] D2: The use of the static keyword is deprecated when declaring objects in namespace scope. Use unnamed namespaces instead as mentioned in this post. static keyword imparts internal linkage to variables/objects in C as well as in C++ in namespace scope as others have mentioned in their posts. P.S: Thie feature has been

typedef'd type not visible as return type of a member function

元气小坏坏 提交于 2019-12-18 17:37:27
问题 This program fails to compile(using gcc-4.5). The error message says: error: ‘myType_t’ does not name a type 1 class abc{ 2 //typedef int myType_t; 3 4 public: 5 typedef int myType_t; 6 7 abc(); 8 myType_t fun1(); 9 }; 10 11 myType_t abc::fun1() 12 { 13 return 0; 14 } 15 16 int main() 17 { 18 abc abc1; 19 return 0; 20 } Now declaring typedef int myType_t; outside the class abc makes this compile. My confusion is, what is the problem if the return type of a member function is typedef'd inside

How do I access variable values from one view controller in another?

六月ゝ 毕业季﹏ 提交于 2019-12-18 17:36:11
问题 I have an integer variable ( time ) in one view controller whose value I need in another view controller. Here's the code: MediaMeterViewController // TRP - On Touch Down event, start the timer -(IBAction) startTimer { time = 0; // TRP - Start a timer timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; [timer retain]; // TRP - Retain timer so it is not accidentally deallocated } // TRP - Method to update the timer display -