473,394 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Possible Compiler Bug (C# 3.0)

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
13 1294

"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
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
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
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


"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
"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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Rob Williscroft | last post by:
I have the following code , part of a trait to detect wether a type has a member operator "Type" (), it works fine with g++ 3.4 (msvc compiles it but it doesn't work for template'd operator T ())...
13
by: Eric | last post by:
I have a template class that instantiates a variable of the template parameter type as follows: template <class T> struct TemplateClass { T t; }; struct TestClass
9
by: Vinodh Kumar P | last post by:
int main() { int aNormalInt = 0; short aShortInt = 0; aShortInt = aNormalInt; // Why I am not getting a warining for possible data lose in MS VC++6.0, even with warning level 4? return 0; } ...
4
by: veereshai | last post by:
i want to copy the functions from my source file into a new file...and convert each function into a new object file by compiling it. now, i want to invoke the function using the object file i have...
2
by: Bret Pehrson | last post by:
I just need to confirm now (after I've spent several days chasing mystery metadata warnings/errors) that this is *NOT* possible: Link a managed C++ static library into a (managed) C# application....
3
by: Chris Calzaretta | last post by:
From: "Chris Calzaretta" <ccalzaretta@hotmail.com> Subject: Re: Is It Possible? Date: Friday, February 04, 2005 11:44 AM Ok i am posting the code I need to create a form from this web service...
24
by: Arne Demmers | last post by:
Hi, As a project of mine I have to do some C programming, which in fact isn't the real problem. The problem it self is that I will have to write some code that is as minimal as possible, and as...
2
by: venkatbo | last post by:
Hi all, I have python2.4 running on ppc-linux 2.6.17. I'm attempting to get a TurboGears 0.9a9 (using CherryPy 2.2.1) based app running on it. During the TG-app startup sequence, it reaches...
1
by: Gerwin | last post by:
Hi, I have been using the interl IPP lib for some time on a microsoft compiler. Now i was wondering if I can switch to a gnu based compiler and still use the IPP. IPP uses .lib files where as...
71
by: Jack | last post by:
I understand that the standard Python distribution is considered the C-Python. Howerver, the current C-Python is really a combination of C and Python implementation. There are about 2000 Python...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.