By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,023 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,023 IT Pros & Developers. It's quick & easy.

Variable vs. child scopes

P: n/a
Hi,

When I try to compile the below code snippet, I get the compile error listed
below in this question. In the lines below the loop, the variable j does not
excist, however, trying to create it is not allowed. If it doesn't excist,
why can't I create it?.

I'm not looking for a solution to this problem - that's quite easy - an
explanation however would be great, as I've been intrigued by this for a
couple of years now.

Thanks Jesper.

Code snippet.

int foo = 0;

for (int j = 0; j < 5; j++)
{
foo += j;
}

int j = 5;
Error 1 A local variable named 'j' cannot be declared in this scope because
it would give a different meaning to 'j', which is already used in a 'child'
scope to denote something else
Oct 24 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jesper, Denmark <Je***********@discussions.microsoft.comwrote:
When I try to compile the below code snippet, I get the compile error listed
below in this question. In the lines below the loop, the variable j does not
excist, however, trying to create it is not allowed. If it doesn't excist,
why can't I create it?.

I'm not looking for a solution to this problem - that's quite easy - an
explanation however would be great, as I've been intrigued by this for a
couple of years now.
The scope of a variable is the whole of the block it's declared in,
even though it's not available before it's declared.

From the Unified C# 3 spec, section 3.7:

<quote>
The scope of a local variable declared in a local-variable-declaration
(§8.5.1) is the block in which the declaration occurs.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 24 '07 #2

P: n/a
Jesper wrote:
Hi,

When I try to compile the below code snippet, I get the compile error listed
below in this question. In the lines below the loop, the variable j does not
excist, however, trying to create it is not allowed. If it doesn't excist,
why can't I create it?.

I'm not looking for a solution to this problem - that's quite easy - an
explanation however would be great, as I've been intrigued by this for a
couple of years now.

Thanks Jesper.

Code snippet.

int foo = 0;

for (int j = 0; j < 5; j++)
{
foo += j;
}

int j = 5;
Error 1 A local variable named 'j' cannot be declared in this scope because
it would give a different meaning to 'j', which is already used in a 'child'
scope to denote something else
A variable in a child scope is not allowed to have the same name as a
variable in the parent scope. Not because the compiler wouldn't be able
to figure it out, but because the code would very easily become
confusing to humans.

Take a simple example where you use a variable in a loop:

int j = 1;
for (int i = 0; i < 10; i++) {
Console.WriteLine(j);
j = 1 - j;
}

If you would change the code to write each value more than once, and
were allowed to use the same variable name i child scope, you could do:

int j = 1;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3; j++) {
Console.WriteLine(j);
}
j = 1 - j;
}

This would of course not give the desired result. The code for the
output hasn't changed, but the meaning of the variable has changed. In a
more complex code, it could be very hard to spot the cause of the problem.

--
Göran Andersson
_____
http://www.guffa.com
Oct 24 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.