c# no concept of scope ?
-
what scewey logic is c# using for scope? why does the second declaration of lenum cause this code not to compile ? but j is ok ?
using System; using System.Collections; public class Test { public static int Main(String[] args) { ArrayList b = new ArrayList(); ArrayList c = new ArrayList(); for( int i = 0; i < b.Count; i++ ) { if( true ) { IEnumerator lenum = b.GetEnumerator(); while( lenum.MoveNext() ) { int j = 0; j++; } } IEnumerator lenum = c.GetEnumerator(); while( lenum.MoveNext() ) { int j = 0; j++; } } return 0; } }
-
what scewey logic is c# using for scope? why does the second declaration of lenum cause this code not to compile ? but j is ok ?
using System; using System.Collections; public class Test { public static int Main(String[] args) { ArrayList b = new ArrayList(); ArrayList c = new ArrayList(); for( int i = 0; i < b.Count; i++ ) { if( true ) { IEnumerator lenum = b.GetEnumerator(); while( lenum.MoveNext() ) { int j = 0; j++; } } IEnumerator lenum = c.GetEnumerator(); while( lenum.MoveNext() ) { int j = 0; j++; } } return 0; } }
My guess is that the C# compile knows that the
if( true )
block will ALWAYS be executed and optimizes theif
statement out. The result is thatlenum = b.GetEnumerator()
andlenum = c.GetEnumerator()
are in the same scope. Roger Stewart "I Owe, I Owe, it's off to work I go..." -
My guess is that the C# compile knows that the
if( true )
block will ALWAYS be executed and optimizes theif
statement out. The result is thatlenum = b.GetEnumerator()
andlenum = c.GetEnumerator()
are in the same scope. Roger Stewart "I Owe, I Owe, it's off to work I go..." -
what scewey logic is c# using for scope? why does the second declaration of lenum cause this code not to compile ? but j is ok ?
using System; using System.Collections; public class Test { public static int Main(String[] args) { ArrayList b = new ArrayList(); ArrayList c = new ArrayList(); for( int i = 0; i < b.Count; i++ ) { if( true ) { IEnumerator lenum = b.GetEnumerator(); while( lenum.MoveNext() ) { int j = 0; j++; } } IEnumerator lenum = c.GetEnumerator(); while( lenum.MoveNext() ) { int j = 0; j++; } } return 0; } }
This is no different to C. Just brace it.
public class Test
{
public static int Main(String[] args)
{
ArrayList b = new ArrayList();
ArrayList c = new ArrayList();
if( true )
{
IEnumerator lenum = b.GetEnumerator();
while( lenum.MoveNext() )
{
int j = 0;
j++;
}
}
{ //see no problem now
IEnumerator lenum = c.GetEnumerator();
while( lenum.MoveNext() )
{
int j = 0;
j++;
}
}
return 0;
}
}leppie::AllocCPArticle("Zee blog");
-
what scewey logic is c# using for scope? why does the second declaration of lenum cause this code not to compile ? but j is ok ?
using System; using System.Collections; public class Test { public static int Main(String[] args) { ArrayList b = new ArrayList(); ArrayList c = new ArrayList(); for( int i = 0; i < b.Count; i++ ) { if( true ) { IEnumerator lenum = b.GetEnumerator(); while( lenum.MoveNext() ) { int j = 0; j++; } } IEnumerator lenum = c.GetEnumerator(); while( lenum.MoveNext() ) { int j = 0; j++; } } return 0; } }
The scopes of the two lenum variables overlap whereas those of the j variables do not. Read section 5.1.7 of the C# language specification. Within the scope of a local variable, it is a compile-time error to refer to the local variable in a textual position that precedes its variable-declarator. However... The lifetime of a local variable is the portion of program execution during which storage is guaranteed to be reserved for it. This lifetime extends from entry into the block, for-statement, switch-statement, or using-statement with which it is associated, until execution of that block, for-statement, switch-statement, or using-statement ends in any way. These two statements together seem to indicate that the compiler is doing what it is suppose to. I wonder why the scope of a variable starts at the entry point of the block and not at the declaration point? Afterall, if you can't reference the variable in a textual position preceding the declaration then why not define its scope in the same manner? I'm sure there's a good answer. I just don't know what it is :) Brian Brian
-
The scopes of the two lenum variables overlap whereas those of the j variables do not. Read section 5.1.7 of the C# language specification. Within the scope of a local variable, it is a compile-time error to refer to the local variable in a textual position that precedes its variable-declarator. However... The lifetime of a local variable is the portion of program execution during which storage is guaranteed to be reserved for it. This lifetime extends from entry into the block, for-statement, switch-statement, or using-statement with which it is associated, until execution of that block, for-statement, switch-statement, or using-statement ends in any way. These two statements together seem to indicate that the compiler is doing what it is suppose to. I wonder why the scope of a variable starts at the entry point of the block and not at the declaration point? Afterall, if you can't reference the variable in a textual position preceding the declaration then why not define its scope in the same manner? I'm sure there's a good answer. I just don't know what it is :) Brian Brian
-
so its not a bug because they say its feature. nice. any other decent language would let this code compile. oh I'll just add it to my list of screwed up things c# does
So C is not decent. Get a life or go back to whatever decent language you were using previously. PS: did you even read my reply? PSS: using a foreach loop allows you to do that. PSSS: You are infact constructing your IEnumerator loop incorrectly. Here is the proper way:
IEnumerable nodes;
....for (IEnumerator enumer = nodes.GetEnumerator(); enumer.MoveNext(); )
{
object node = enumer.Current;
...
}leppie::AllocCPArticle("Zee blog");
-
what scewey logic is c# using for scope? why does the second declaration of lenum cause this code not to compile ? but j is ok ?
using System; using System.Collections; public class Test { public static int Main(String[] args) { ArrayList b = new ArrayList(); ArrayList c = new ArrayList(); for( int i = 0; i < b.Count; i++ ) { if( true ) { IEnumerator lenum = b.GetEnumerator(); while( lenum.MoveNext() ) { int j = 0; j++; } } IEnumerator lenum = c.GetEnumerator(); while( lenum.MoveNext() ) { int j = 0; j++; } } return 0; } }
You could try using a compiler that doesn't punish good programmers for using perfectly legitimate code. Technically, there is no conflict so long as the outer scope lenum comes after the inner scope one. MS's csc and Mono's mcs both use this buggy scoping, however DotGNU Portable.NET's cscc allows this code (as well it should) since there is no conflict. It does reject code where the outer scope variable is declared before the inner scope variable, however (since in that case there is an actual conflict). Rich P.S. For more info on DotGNU check out: http://www.dotgnu.org/
-
So C is not decent. Get a life or go back to whatever decent language you were using previously. PS: did you even read my reply? PSS: using a foreach loop allows you to do that. PSSS: You are infact constructing your IEnumerator loop incorrectly. Here is the proper way:
IEnumerable nodes;
....for (IEnumerator enumer = nodes.GetEnumerator(); enumer.MoveNext(); )
{
object node = enumer.Current;
...
}leppie::AllocCPArticle("Zee blog");
leppie wrote: So C is not decent. Get a life or go back to whatever decent language you were using previously. you would think a made i made a comment about your mother. PS: did you even read my reply? i did read your reply but you didn't explain the problem you just posted a work around. PSSS: You are infact constructing your IEnumerator loop incorrectly. Here is the proper way: incorrectly ? there is very little difference between the two and in my opinion the while loop is more readably, you don't have an empty statement as in your for loop. what's the deal anyways. i make a couple of comments about the quirks in the language( every language has them ) and you go off on me like i declare C# as the worst language in the world and every programmer who uses is stupid. now if i have offended anyone, i apologize. but you should remember its just another programming language. i don't think knights running around defending its honour are necessary.
-
So C is not decent. Get a life or go back to whatever decent language you were using previously. PS: did you even read my reply? PSS: using a foreach loop allows you to do that. PSSS: You are infact constructing your IEnumerator loop incorrectly. Here is the proper way:
IEnumerable nodes;
....for (IEnumerator enumer = nodes.GetEnumerator(); enumer.MoveNext(); )
{
object node = enumer.Current;
...
}leppie::AllocCPArticle("Zee blog");
-
leppie wrote: So C is not decent. Get a life or go back to whatever decent language you were using previously. you would think a made i made a comment about your mother. PS: did you even read my reply? i did read your reply but you didn't explain the problem you just posted a work around. PSSS: You are infact constructing your IEnumerator loop incorrectly. Here is the proper way: incorrectly ? there is very little difference between the two and in my opinion the while loop is more readably, you don't have an empty statement as in your for loop. what's the deal anyways. i make a couple of comments about the quirks in the language( every language has them ) and you go off on me like i declare C# as the worst language in the world and every programmer who uses is stupid. now if i have offended anyone, i apologize. but you should remember its just another programming language. i don't think knights running around defending its honour are necessary.
jpribele wrote: now if i have offended anyone, i apologize. but you should remember its just another programming language. i don't think knights running around defending its honour are necessary. I think I have misunderstood too. :doh: (rough week/boring weekend) I apologize. From the post below by Rich333 (pnet developer) he clarified it. :) It appears to be a EMCA spec interpretation problem. leppie::AllocCPArticle("Zee blog");
Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.