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

Possible Compiler Bug (C# 3.0)

P: n/a
The attached program does not compile. The error message says:
"A local variable named 'i' cannot be declared in this scope because it
would give a different meaning to 'i', which is already used in a 'child'
scope to denote something else"
- which does not make much sense. Is it a compiles bug?
TJ

class Program
{
static void Main(string[] args)
{
switch (0) {
case 0:
for (int i = 0; ; ) {
}
case 1:
// error CS0136: A local variable named 'i' cannot be declared
in this scope because it would give a different meaning to 'i', which is
already used in a 'child' scope to denote something else
int i = 0;
}
}
}
Jul 9 '08 #1
Share this Question
Share on Google+
13 Replies


P: n/a

"Tomasz J" <oe****@nospam.nospamwrote in message
news:eg**************@TK2MSFTNGP06.phx.gbl...
The attached program does not compile. The error message says:
"A local variable named 'i' cannot be declared in this scope because it
would give a different meaning to 'i', which is already used in a 'child'
scope to denote something else"
- which does not make much sense. Is it a compiles bug?
TJ

class Program
{
static void Main(string[] args)
{
switch (0) {
case 0:
for (int i = 0; ; ) {
}
case 1:
Console.Writeline( i.ToString());
//if the above compiles, and it shows the "i" value, then it is
IN_SCOPE and already declared.
//int i = 0;
}
}
}

Jul 9 '08 #2

P: n/a
JPK
e-gads ! I thought the days of x,y,z,j,k,i were dead and gone. Should we
call this style 'retro-code'? ;-)

"Tomasz J" <oe****@nospam.nospamwrote in message
news:eg**************@TK2MSFTNGP06.phx.gbl...
The attached program does not compile. The error message says:
"A local variable named 'i' cannot be declared in this scope because it
would give a different meaning to 'i', which is already used in a 'child'
scope to denote something else"
- which does not make much sense. Is it a compiles bug?
TJ

class Program
{
static void Main(string[] args)
{
switch (0) {
case 0:
for (int i = 0; ; ) {
}
case 1:
// error CS0136: A local variable named 'i' cannot be declared
in this scope because it would give a different meaning to 'i', which is
already used in a 'child' scope to denote something else
int i = 0;
}
}
}
Jul 9 '08 #3

P: n/a
This is not a bug, the scope of a variable define in a switch block is the
entire switch block. This is, i supposed, to help people who use the goto
case or goto default functionality.
for more information have a look at:
http://msdn.microsoft.com/en-us/libr...49(VS.71).aspx

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"Tomasz J" wrote:
The attached program does not compile. The error message says:
"A local variable named 'i' cannot be declared in this scope because it
would give a different meaning to 'i', which is already used in a 'child'
scope to denote something else"
- which does not make much sense. Is it a compiles bug?
TJ

class Program
{
static void Main(string[] args)
{
switch (0) {
case 0:
for (int i = 0; ; ) {
}
case 1:
// error CS0136: A local variable named 'i' cannot be declared
in this scope because it would give a different meaning to 'i', which is
already used in a 'child' scope to denote something else
int i = 0;
}
}
}
Jul 9 '08 #4

P: n/a
yes, you definitely should

"JPK" <ja***@klett.uswrote in message
news:58**********************************@microsof t.com...
e-gads ! I thought the days of x,y,z,j,k,i were dead and gone. Should we
call this style 'retro-code'? ;-)

"Tomasz J" <oe****@nospam.nospamwrote in message
news:eg**************@TK2MSFTNGP06.phx.gbl...
>The attached program does not compile. The error message says:
"A local variable named 'i' cannot be declared in this scope because it
would give a different meaning to 'i', which is already used in a 'child'
scope to denote something else"
- which does not make much sense. Is it a compiles bug?
TJ

class Program
{
static void Main(string[] args)
{
switch (0) {
case 0:
for (int i = 0; ; ) {
}
case 1:
// error CS0136: A local variable named 'i' cannot be declared
in this scope because it would give a different meaning to 'i', which is
already used in a 'child' scope to denote something else
int i = 0;
}
}
}

Jul 9 '08 #5

P: n/a


"JPK" <ja***@klett.uswrote in message
news:58**********************************@microsof t.com...
e-gads ! I thought the days of x,y,z,j,k,i were dead and gone. Should we
call this style 'retro-code'? ;-)

<snip>

Heck no, I still use i, j, k, x, y, and z variables because when developers
see those, they usually understand that they represent a counter in the
enclosing block (or on of it's ancestor blocks). To take a single counter,
'i', and name it 'thisIsMyCounter' is IMHO, just plain rude lol. Why force
someone to type all that out when all it is is a counter?

HTH,
Mythran
Jul 9 '08 #6

P: n/a
"Tomasz J" <oe****@nospam.nospamwrote in message
news:eg**************@TK2MSFTNGP06.phx.gbl...
The attached program does not compile. The error message says:
"A local variable named 'i' cannot be declared in this scope because it
would give a different meaning to 'i', which is already used in a 'child'
scope to denote something else"
- which does not make much sense. Is it a compiles bug?
TJ

class Program
{
static void Main(string[] args)
{
switch (0) {
case 0:
for (int i = 0; ; ) {
}
case 1:
// error CS0136: A local variable named 'i' cannot be declared
in this scope because it would give a different meaning to 'i', which is
already used in a 'child' scope to denote something else
int i = 0;
}
}
}
It's not a bug, it's a documented feature. Contrary to other "flavors" of
C, in C# it is not allowed to have a variable in a nested block named the
same as a variable in its containing block. For instance, the following
would compile in traditional C, but not in C#:

while (condition1)
{
int i;
...
while (condition2)
{
int i;
...
}
}

The reasoning is that this used to lead to many programming errors. If
you access "i" in the inner block, it refers to the "i" that is declared
inside that same block, but in a larger program it is easy to make a mistake
and think that it is referring to the outer "i". To avoid this risk, the C#
designers decided to disallow this situation.

Jul 9 '08 #7

P: n/a
JPK
If Collection contains ;

Cars -"CarCounter"
Animals -"AnimalCounter"
People -"PersonCounter"
When nesting multiple Scopes it can be hard to recognize which scope you
are in when all you have if i,j,k. If I see "CarCounter" I know exactly
what scope I am in .

JIM
"Mythran" <My*****@community.nospamwrote in message
news:24**********************************@microsof t.com...
>

"JPK" <ja***@klett.uswrote in message
news:58**********************************@microsof t.com...
>e-gads ! I thought the days of x,y,z,j,k,i were dead and gone. Should
we call this style 'retro-code'? ;-)

<snip>

Heck no, I still use i, j, k, x, y, and z variables because when
developers see those, they usually understand that they represent a
counter in the enclosing block (or on of it's ancestor blocks). To take a
single counter, 'i', and name it 'thisIsMyCounter' is IMHO, just plain
rude lol. Why force someone to type all that out when all it is is a
counter?

HTH,
Mythran

Jul 9 '08 #8

P: n/a
MC
>e-gads ! I thought the days of x,y,z,j,k,i were dead and gone. Should
>we call this style 'retro-code'? ;-)
Heck no, I still use i, j, k, x, y, and z variables because when
developers see those, they usually understand that they represent a
counter in the enclosing block (or on of it's ancestor blocks). To take a
single counter, 'i', and name it 'thisIsMyCounter' is IMHO, just plain
rude lol. Why force someone to type all that out when all it is is a
counter?
Right! For variables of very local significance I use:

i, j, k for integers
x, y, z for floats/doubles
c, c1, c2... for characters
s, s1, s2... for strings
b, b1, b2... for booleans (rare)
Jul 9 '08 #9

P: n/a
It's really funny.. my app got compiled and I got only warning as

"Warning 1 The variable 'i' is assigned but its value is never usedD:\Pract\FirstConsoleApp\FirstConsoleApp\Progr am.cs 13 30 FirstConsoleApp"But when I run the app, it is endup with infinite loop.

What about others.. did any one tried compiling and running the app?
"Tomasz J" <oe****@nospam.nospamwrote in message
news:eg**************@TK2MSFTNGP06.phx.gbl...
The attached program does not compile. The error message says:
"A local variable named 'i' cannot be declared in this scope because it
would give a different meaning to 'i', which is already used in a 'child'
scope to denote something else"
- which does not make much sense. Is it a compiles bug?
TJ

class Program
{
static void Main(string[] args)
{
switch (0) {
case 0:
for (int i = 0; ; ) {
}
case 1:
// error CS0136: A local variable named 'i' cannot be declared
in this scope because it would give a different meaning to 'i', which is
already used in a 'child' scope to denote something else
int i = 0;
}
}
}
Jul 10 '08 #10

P: n/a
On Jul 10, 1:14*pm, "Chakravarthy" <dskch...@msn.comwrote:
It's really funny.. my app got compiled and I got only warning as

"Warning * 1 * * * The variable 'i' is assigned but its value is never usedD:\Pract\FirstConsoleApp\FirstConsoleApp\Progr am.cs * * 13 * * *30 * * *FirstConsoleApp"But when I run the app, it is endup with infinite loop.

What about others.. did any one tried compiling and running the app?
My guess is that you're using Mono. (Mind you, in Mono I get 3
warnings, not just one.) The fact that it compiles under mcs is a bug
in mcs, in my view. The spec's pretty clear on this front, I think.

(And yes, if you run it it certainly will give an infinite loop due to
the nature of the for loop.)

Jon
Jul 10 '08 #11

P: n/a
Mythran wrote:
Heck no, I still use i, j, k, x, y, and z variables because when
developers see those, they usually understand that they represent a
counter in the enclosing block (or on of it's ancestor blocks). To take
a single counter, 'i', and name it 'thisIsMyCounter' is IMHO, just plain
rude lol. Why force someone to type all that out when all it is is a
counter?
If it actually made the code more readable then it would be worth to
force them.

But the fact is that i,j,k has become a standard for for loop
counters.

So even though the variable names are short they actually tell
the reader a lot.

Arne
Jul 14 '08 #12

P: n/a
Mythran wrote:
Heck no, I still use i, j, k, x, y, and z variables because when
developers see those, they usually understand that they represent a
counter in the enclosing block (or on of it's ancestor blocks). To take
a single counter, 'i', and name it 'thisIsMyCounter' is IMHO, just plain
rude lol. Why force someone to type all that out when all it is is a
counter?
If it actually made the code more readable then it would be worth to
force them.

But the fact is that i,j,k has become a standard for for loop
counters.

So even though the variable names are short they actually tell
the reader a lot.

Arne
Jul 14 '08 #13

P: n/a
Mythran wrote:
Heck no, I still use i, j, k, x, y, and z variables because when
developers see those, they usually understand that they represent a
counter in the enclosing block (or on of it's ancestor blocks). To take
a single counter, 'i', and name it 'thisIsMyCounter' is IMHO, just plain
rude lol. Why force someone to type all that out when all it is is a
counter?
If it actually made the code more readable then it would be worth to
force them.

But the fact is that i,j,k has become a standard for for loop
counters.

So even though the variable names are short they actually tell
the reader a lot.

Arne
Jul 14 '08 #14

This discussion thread is closed

Replies have been disabled for this discussion.