variable hides a variable in an enclosing block error - vb.net

The code
Using m_objSqlConnection = New SqlCeConnection(m_strConnectionString)
End Using
produces the following error:
Variable 'm_objSqlConnection' hides a variable in an enclosing block

The "m_" prefix suggests that you have a member variable with that name. When you use a Using statement you are declaring a variable that exists only in the scope of that block. If you already have a member variable with that name then why would you want a local variable with the same name? You need to decide whether a local variable or a member variable is more appropriate and stick with the one option. If you're disposing the connection at the end of the Using block then you're apparently not reusing it so I'd think that getting rid of the member variable is the way to go.

Related

What are anonymous variables?

A program variable is an abstraction of a computer memory cell or collection
of cells. Programmers often think of variable names as names for memory locations, but there is much more to a variable than just a name.
In this case, what is an anonymous variable?
What does the below statement mean?
Variables without names are called anonymous variables.
Can you provide language specific examples for the same?
In C++, reference variable to const can be initialized by constant.
In this point, temporary variable is created in memory to grab the constant.
const int &ref = 3;
like this. so we can call this temporary variable to "anonymous variable".
Variables are where you store you values. 'Variable Name' is the usually the easiest (and more human-like) way to locate your value.For example, if I am a variable, you can get my value by calling my name, and the combination of my value and my name is called 'variable'.
However, not all variables need a name.Sometimes you just use them once and don't need them anymore; and in that case, a name is unnecessary.
The example given by #BAE HA RAM is a telling one,in which case you don't need a name for the value but ref to it by a pointer(But still got a name for that pointer)..
There are also many other anonymous things, anonymous type, anonymous function and so on. Most of them are created to avoid too many meaningless names for the things that you only need to run once.
I'd like to know which language you are using, so more specific example can be given...

Julia: Variable not defined

The variable scope behavior seems quite strange. The code block
tp = 1
function test2()
println(tp)
end
works perfectly well while
function test()
if tp==0
tp=tp-1
end
end
gives the exception "tp not defined". What is wrong?
This is tricky due to the way variables are implicitly defined as local or global, and the fact that definitions later in a function can affect their scoping in the whole function.
In the first case, tp defaults to being a global variable, and it works as you expected. However, in the second case, you assign to tp. This, as is noted in the scope of variables section of the manual:
"An assignment x = y introduces a new local variable x only if x is neither declared global nor introduced as local by any enclosing scope before or after the current line of code."
So, by assigning to tp, you've implicitly declared it as a local variable! It will now shadow the definition of your global… except that you try to access it first. The solution is simple: explicitly declare any variables to be global if you want to assign to them:
function test()
global tp
if tp==0
tp=tp-1
end
end
The behavior here is finely nuanced, but it's very consistent. I know it took me a few reads through that part of the manual before I finally understood how this works. If you can think of a better way to describe it, please say something!

How to declare a variable without initting it to nil in Lua?

I've found it very useful to do something like:
if not variable then
variable = value
end
Of course, I'd usually rather that variable was local, but I can't declare it local in my if, or it won't be accessible.
So sometimes I do:
local variable
if not variable then
variable = value
end
The problem is that when I iterate over this code, the variable declaration sets the variable equal to nil. If I can live with a global value (I can), I can get around it by just not declaring the variable outside of the if block.
But isn't there some way that I can both have my local value and let it keep its value?
First off, the way or is defined in Lua gives you a nice idiom to avoid the if altogether:
variable = variable or value
If variable is nil, or will evaluate to its second operand. Of course, this will only work, if false is not a valid value for variable (because false and nil are both "false" as far as or is concerned).
However, you still have the problem that you need to declare the variable somewhere. I suppose your problem is that in the case of a global loop you think you have to do either:
while condition do
variable = variable or value
process(variable)
end
(which would make variable global) or
while condition do
local variable
variable = variable or value
process(variable)
end
which is pointless because local limits the scope to one iteration and reinitializes variable as `nil.
What you can do though is create another block that limits the scope of local variables but does nothing else:
do
local variable
while condition do
variable = variable or value
process(variable)
end
end
There's a simple way to test the scope of locals inside loops:
local a = "this is external a"
for x = 1, 3 do
print(a)
local a = "THIS IS INTERNAL a"
print(a)
end
If you run this you'll get:
this is external a
THIS IS INTERNAL a
this is external a
THIS IS INTERNAL a
this is external a
THIS IS INTERNAL a
If the local a inside the loop survived to the next iteration then after the first print it should have printed only "THIS IS INTERNAL a" as the internal a would shadow the external a. The two strings alternating means that the internal local a never survives the bottom of the loop.
Of course, even if it did survive you would have the problem of the internal local declaration shadowing the previous iteration's local a each time through the loop. Essentially you would be unable to access the previous iteration's version of a and by the third iteration you would have 3 separate internal locals, only the last of which would be accessible. It would be the same as if you had done this:
local a = "THIS IS INTERNAL a"
local a = "THIS IS INTERNAL a"
local a = "THIS IS INTERNAL a"

Function/variable scope (pass by value or reference?)

I'm completely confused by Lua's variable scoping and function argument passing (value or reference).
See the code below:
local a = 9 -- since it's define local, should not have func scope
local t = {4,6} -- since it's define local, should not have func scope
function moda(a)
a = 10 -- creates a global var?
end
function modt(t)
t[1] = 7 -- create a global var?
t[2] = 8
end
moda(a)
modt(t)
print(a) -- print 9 (function does not modify the parent variable)
print(t[1]..t[2]) -- print 78 (some how modt is modifying the parent t var)
As such, this behavior completely confuses me.
Does this mean that table variables
are passed to the function by
reference and not value?
How is the global variable creation
conflicting with the already define
local variable?
Why is modt able to
modify the table yet moda is not able
to modify the a variable?
You guessed right, table variables are passed by reference. Citing Lua 5.1 Reference Manual:
There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table.
....
Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy.
So nil, booleans, numbers and strings are passed by value. This exactly explains the behavior you observe.
Lua's function, table, userdata and thread (coroutine) types are passed by reference. The other types are passed by value. Or as some people like to put it; all types are passed by value, but function, table, userdata and thread are reference types.
string is also a kind of reference type, but is immutable, interned and copy-on-write - it behaves like a value type, but with better performance.
Here's what's happening:
local a = 9
local t = {4,6}
function moda(a)
a = 10 -- sets 'a', which is a local introduced in the parameter list
end
function modt(t)
t[1] = 7 -- modifies the table referred to by the local 't' introduced in the parameter list
t[2] = 8
end
Perhaps this will put things into perspective as to why things are the way they are:
local a = 9
local t = {4,6}
function moda()
a = 10 -- modifies the upvalue 'a'
end
function modt()
t[1] = 7 -- modifies the table referred to by the upvalue 't'
t[2] = 8
end
-- 'moda' and 'modt' are closures already containing 'a' and 't',
-- so we don't have to pass any parameters to modify those variables
moda()
modt()
print(a) -- now print 10
print(t[1]..t[2]) -- still print 78
jA_cOp is correct when he says "all types are passed by value, but function, table, userdata and thread are reference types."
The difference between this and "tables are passed by reference" is important.
In this case it makes no difference,
function modt_1(x)
x.foo = "bar"
end
Result: both "pass table by reference" and "pass table by value, but table is a reference type" will do the same: x now has its foo field set to "bar".
But for this function it makes a world of difference
function modt_2(x)
x = {}
end
In this case pass by reference will result in the argument getting changed to the empty table. However in the "pass by value, but its a reference type", a new table will locally be bound to x, and the argument will remain unchanged. If you try this in lua you will find that it is the second (values are references) that occurs.
I won't repeat what has already been said on Bas Bossink and jA_cOp's answers about reference types, but:
-- since it's define local, should not have func scope
This is incorrect. Variables in Lua are lexically scoped, meaning they are defined in a block of code and all its nested blocks.
What local does is create a new variable that is limited to the block where the statement is, a block being either the body of a function, a "level of indentation" or a file.
This means whenever you make a reference to a variable, Lua will "scan upwards" until it finds a block of code in which that variable is declared local, defaulting to global scope if there is no such declaration.
In this case, a and t are being declared local but the declaration is in global scope, so a and t are global; or at most, they are local to the current file.
They are then not being redeclared local inside the functions, but they are declared as parameters, which has the same effect. Had they not been function parameters, any reference inside the function bodies would still refer to the variables outside.
There's a Scope Tutorial on lua-users.org with some examples that may help you more than my attempt at an explanation. Programming in Lua's section on the subject is also a good read.
Does this mean that table variables are passed to the function by reference and not value?
Yes.
How is the global variable creation conflicting with the already define local variable?
It isn't. It might appear that way because you have a global variable called t and pass it to a function with an argument called t, but the two ts are different. If you rename the argument to something else, e,g, q the output will be exactly the same. modt(t) is able to modify the global variable t only because you are passing it by reference. If you call modt({}), for example, the global t will be unaffected.
Why is modt able to modify the table yet moda is not able to modify the a variable?
Because arguments are local. Naming your argument a is similar to declaring a local variable with local a except obviously the argument receives the passed-in value and a regular local variable does not. If your argument was called z (or was not present at all) then moda would indeed modify the global a.

Use same name for local variable and function

In VBA, I want to use a name for a local variable that I'd also like to use for a function name. The problem I'd that the function name formatting always changes to the local variable formatting.
Any way to prevent that?
I highly recommend not using the same name for disambiguation purposes. Also, if VBA is not case sensitive, it may not know whether you are referring to the function or the variable and thus give a runtime error (I don't think it is compiled per se, but it goes to a proprietary p-code intermediate.)
Often when you'd like the names to be similar, it can be useful to prepend an underscore to one, such as a local variable. Thus I recommend you name the function FunctionName and the variable _FunctionName if you want to go that route.
If you want to try having the same name for each, you will likely need to edit the code outside of the IDE that is reformatting your code. In an editor that doesn't try to auto-format, you may be able to force it. Then whether it compiles or not is the question.
In Visual Basic, each function already has a variable named after the function. Assigning a value to this variable is the only way to set a return value for this function.
Therefore, declaring yet another variable of the same name creates ambiguity. You cannot do so. It results in the Duplicate declaration in current scope error.
And if by "local" variable you meant a module-level variable, then you cannot do it either: Ambiguous name detected: %s.
And if you are asking about a situation when a function and a variable of the same name belong to different scopes, then VBA will use the case of the line that was edited last. So if you declare a function and then declare a variable in another module, the function name will change case. But if you then return to the function, change its casing back and press Enter, the variable, in its turn, will change its casing to match the function name.