scope

internal methods and data structures . .

if i have a protected method, can i pass in a parameter where the data type is declared internal? ...

Ensuring MySQL connection works in PHP function

I have code with the following form: <?php function doSomething{ //Do stuff with MySQL $con->tralalala(); } $con = connectToDatabase;//This would actually be a line or two. doSomething(); ?> This (type of) code doesn't work, because doSomething() doesn't have a connection to the database. Can anyone explain why not? I create the...

Python globals, locals, and UnboundLocalError

I ran across this case of UnboundLocalError recently, which seems strange: import pprint def main(): if 'pprint' in globals(): print 'pprint is in globals()' pprint.pprint('Spam') from pprint import pprint pprint('Eggs') if __name__ == '__main__': main() Which produces: pprint is in globals() Traceback (most recent ...

How can one de-reference JavaScript variables when enclosing an outer scope

Ok, here's a problem script. var links = [ 'one', 'two', 'three' ]; for( var i = 0; i < links.length; i++ ) { var a = document.createElement( 'div' ); a.innerHTML = links[i]; a.onclick = function() { alert( i ) } document.body.appendChild( a ); } This script generates three divs: one, two and three, using an array. I'...

How do I create a list of Python lambdas (in a list comprehension/for loop)?

I want to create a list of lambda objects from a list of constants in Python; for instance: listOfNumbers = [1,2,3,4,5] square = lambda x: x * x listOfLambdas = [lambda: square(i) for i in listOfNumbers] This will create a list of lambda objects, however, when I run them: for f in listOfLambdas: print f(), I would expect that i...

In XSLT how do I access elements from the outer loop from within nested loops?

I have nested xsl:for loops: <xsl:for-each select="/Root/A"> <xsl:for-each select="/Root/B"> <!-- Code --> </xsl:for> </xsl:for> From within the inner loop, how can I access attributes from the current node in the outer loop? I keep finding myself writing code like this: <xsl:for-each select="/Root/A"> <xsl:varia...

Private inner classes in C# - why aren't they used more often ?

I am relatively new to C# and each time I begun to work on a C# project (I only worked on nearly mature projects in C#) - I'm wondering why there is no inner classes. Maybe I don't understand their goal. To me, inner classes -- at least private inner classes -- look a lot like "inner procedures" in Pascal / Modula-2 / Ada : they allow t...

Javascript: how to set "this" variable easily?

I have a pretty good understanding of Javascript, except that I can't figure out a nice way to set the "this" variable. Consider: var myFunction = function(){ alert(this.foo_variable); } var someObj = document.body; //using body as example object someObj.foo_variable = "hi"; //set foo_variable so it alerts var old_fn = someObj.fn...

How to tell a project manager "NO" to scope creep

While project managers may each have their own personality and management style, it seems that many of them have a pernicious love of sneaking in "scope creep" when they can (whether anyone is watching or not). While they usually mean well (bless their hearts), what's the best way that you've found to say "NO" to project managers? ...

Using an STL Iterator without initialising it

I would like to do something like this: container::iterator it = NULL; switch ( eSomeEnum ) { case Container1: it = vecContainer1.begin(); break; case Container2: it = vecContainer2.begin(); break; ... } for( ; it != itEnd ; ++it ) { .. } But I can't create and initialise an iterator to NULL. Is there some way I can do this? Ide...

Does Perl monkey-patching allow you to see the patched package's scope?

I'm monkey patching a package using a technique given at the beginning of "How can I monkey-patch an instance method in Perl?". The problem that I'm running into is that the original subroutine used a package-level my variable which the patched subroutine appears not to have access to, either by full path specification or implicit use. ...

Coding practice: return by value or by reference in Matrix multiplication?

I'm writing this question with reference to this one which I wrote yesterday. After a little documentation, it seems clear to me that what I wanted to do (and what I believed to be possible) is nearly impossible if not impossible at all. There are several ways to implement it, and since I'm not an experienced programmer, I ask you which ...

Basic C++ question regarding scope of functions

I'm just starting to learn C++ so you'll have to bear with my ignorance. Is there a way to to declare functions such that they can be referenced without being written before the function that uses them. I'm working with one cpp file (not my decision) and my functions call themselves so there is not really a proper order to place them in....

scope in PHP classes

I am creating a database connection object, and I'm not sure what the best way would be... If I create a dbConnection class which establishes the connection (using mysql_connect) can I then call mysql_query from anywhere in the main script? what about from inside OTHER classes? Do i need to pass the object or attribute as parameters? ...

After restricting Setter scope and then applying an interface, scope is disregarded!

If I set a Friend-level scope on a setter, like this... Public Class MyClass Public Property IsDirty() As Boolean Get Return _isDirty End Get Friend Set(ByVal trueFalse As Boolean) _isDirty = trueFalse End Set End Property End Class ...And then call it from another proje...

Controlling the value of 'this' in a jQuery event

I have created a 'control' using jQuery and used jQuery.extend to assist in making it as OO as possible. During the initialisation of my control I wire up various click events like so jQuery('#available input', this.controlDiv).bind('click', this, this.availableCategoryClick); Notice that I am pasing 'this' as the data a...

Does procedural programming have any advantages over OOP?

[Edit:] Earlier I asked this as a perhaps poorly-framed question about when to use OOP versus when to use procedural programming - some responses implied I was asking for help understanding OOP. On the contrary, I have used OOP a lot but want to know when to use a procedural approach. Judging by the responses, I take it that there is a f...

PHP define scope for included file

I have quite a lot of PHP view files, which I used to include in my controllers using simple include statements. They all use methods declared in a view class which they get like $view->method(); However I recently decided that it would be better if the including would also be done by this view class. This however changes the scope of th...

Control scope within Repeater, with and without UpdatePanel

Why does the following give me a compilation error for line B (Label2, outside UpdatePanel) but not for line A (Label1, inside UpdatePanel)? I would have expected both lines to give an error since both controls are within the same Repeater and should therefore not be directly accessible outside the repeater, since there is no one unique ...

How can one create new scopes in python

In many languages (and places) there is a nice practice of creating local scopes by creating a block like this. void foo() { ... Do some stuff ... if(TRUE) { char a; int b; ... Do some more stuff ... } ... Do even more stuff ... } How can I implement this in python without getti...