446,198 Members | 925 Online
Need help? Post your question and get tips & solutions from a community of 446,198 IT Pros & Developers. It's quick & easy.

# scope question :

 P: n/a for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...? Nov 15 '05 #1
23 Replies

 P: n/a Yep. "NotYetaNurd" wrote in message news:ec**************@TK2MSFTNGP10.phx.gbl... for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...? Nov 15 '05 #2

 P: n/a yes you go ... that is only from the things you have define within the scope of the for loop Nirosh. "NotYetaNurd" wrote in message news:ec**************@TK2MSFTNGP10.phx.gbl... for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...? Nov 15 '05 #3

 P: n/a when i tried this for(int i=0;i<6;i++) { for(int j=0;j wrote in message news:ec**************@TK2MSFTNGP10.phx.gbl... for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...? Nov 15 '05 #4

 P: n/a that is since for (int i=0; i<6; i++) === int i; for(i=0; i<6; i+) Nirosh. "NotYetaNurd" wrote in message news:#1**************@TK2MSFTNGP10.phx.gbl... when i tried this for(int i=0;i<6;i++) { for(int j=0;j wrote in message news:ec**************@TK2MSFTNGP10.phx.gbl... for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...? Nov 15 '05 #5

 P: n/a in that case following should work rite for(int i=0;i<6;i++) { } i = 1; it gives the following error The name 'i' does not exist in the class or namespace 'JTaskMgr.MainForm' "Champika Nirosh" wrote in message news:uK**************@TK2MSFTNGP10.phx.gbl... that is since for (int i=0; i<6; i++) === int i; for(i=0; i<6; i+) Nirosh. "NotYetaNurd" wrote in message news:#1**************@TK2MSFTNGP10.phx.gbl... when i tried this for(int i=0;i<6;i++) { for(int j=0;j wrote in message news:ec**************@TK2MSFTNGP10.phx.gbl... for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...? Nov 15 '05 #6

 P: n/a Yes that won't work and let me explain it this way Even if the scope of an element is limited to the block, its lifetime is still that of the entire procedure. Therefore when you define the int i in the procedure it give a different meaning to the variable i define in the child scope. but if you define your new i in a another block that will prevent you from the reported error Nirosh. "NotYetaNurd" wrote in message news:#a**************@tk2msftngp13.phx.gbl... in that case following should work rite for(int i=0;i<6;i++) { } i = 1; it gives the following error The name 'i' does not exist in the class or namespace 'JTaskMgr.MainForm' "Champika Nirosh" wrote in message news:uK**************@TK2MSFTNGP10.phx.gbl... that is since for (int i=0; i<6; i++) === int i; for(i=0; i<6; i+) Nirosh. "NotYetaNurd" wrote in message news:#1**************@TK2MSFTNGP10.phx.gbl... when i tried this for(int i=0;i<6;i++) { for(int j=0;j wrote in message news:ec**************@TK2MSFTNGP10.phx.gbl... > for(int i=0;i<6;i++) > { > // > } > > wont i go out of scope after the for loop ...? > > Nov 15 '05 #7

 P: n/a NotYetaNurd wrote: when i tried this for(int i=0;i<6;i++) { for(int j=0;j http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 15 '05 #8

 P: n/a Champika Nirosh wrote: that is since for (int i=0; i<6; i++) === int i; for(i=0; i<6; i+) No it's not. See another reply for a fuller explanation. -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 15 '05 #9

 P: n/a Champika Nirosh wrote: Yes that won't work and let me explain it this way Even if the scope of an element is limited to the block, its lifetime is still that of the entire procedure. No it's not. It's the lifetime of the *block*. This, for instance compiles fine: using System; class Test { static void Main() { { int i=5; Console.WriteLine (i); } { int i=10; Console.WriteLine (i); } { for (int i=0; i < 5; i++) { Console.WriteLine (i); } } } } -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 15 '05 #10

 P: n/a What proof you have to say that the the variable (which define in a block) lifetime is not equal to that of the entire procedure. your example is not capable of talking about the life time and I am not talking about the SCOPE. Nirosh. "Jon Skeet [C# MVP]" wrote in message news:MP************************@msnews.microsoft.c om... Champika Nirosh wrote: Yes that won't work and let me explain it this way Even if the scope of an element is limited to the block, its lifetime is still that of the entire procedure. No it's not. It's the lifetime of the *block*. This, for instance compiles fine: using System; class Test { static void Main() { { int i=5; Console.WriteLine (i); } { int i=10; Console.WriteLine (i); } { for (int i=0; i < 5; i++) { Console.WriteLine (i); } } } } -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 15 '05 #11

 P: n/a if it has the scope for the entire block the following should compile rite { for (int i=0; i < 6; i++) { ... } i=1; } but am getting " The name 'i' does not exist in the class or namespace......." "Jon Skeet [C# MVP]" wrote in message news:MP************************@msnews.microsoft.c om... NotYetaNurd wrote: when i tried this for(int i=0;i<6;i++) { for(int j=0;j http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 15 '05 #12

 P: n/a If i defined in the procudure has the scope for its entire procedure I should be able to do some thing like i = 0; int i; but this is not possible why? Nirosh. "Jon Skeet [C# MVP]" wrote in message news:MP************************@msnews.microsoft.c om... NotYetaNurd wrote: when i tried this for(int i=0;i<6;i++) { for(int j=0;j http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 15 '05 #13

 P: n/a NotYetaNurd wrote: if it has the scope for the entire block the following should compile rite { for (int i=0; i < 6; i++) { ... } i=1; } but am getting " The name 'i' does not exist in the class or namespace......." No, because the block of the variable is the for loop - that is a block itself. -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 15 '05 #14

 P: n/a Please Read http://msdn.microsoft.com/library/de...us/vbcn7/html/ vbconscopelevels.asp Nirosh "Champika Nirosh" wrote in message news:u3**************@TK2MSFTNGP11.phx.gbl... What proof you have to say that the the variable (which define in a block) lifetime is not equal to that of the entire procedure. your example is not capable of talking about the life time and I am not talking about the SCOPE. Nirosh. "Jon Skeet [C# MVP]" wrote in message news:MP************************@msnews.microsoft.c om... Champika Nirosh wrote: Yes that won't work and let me explain it this way Even if the scope of an element is limited to the block, its lifetime is still that of the entire procedure. No it's not. It's the lifetime of the *block*. This, for instance compiles fine: using System; class Test { static void Main() { { int i=5; Console.WriteLine (i); } { int i=10; Console.WriteLine (i); } { for (int i=0; i < 5; i++) { Console.WriteLine (i); } } } } -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 15 '05 #16

 P: n/a Champika Nirosh wrote: If i defined in the procudure has the scope for its entire procedure I should be able to do some thing like i = 0; int i; but this is not possible why? Because you can't *use* variables "out of order" - they have that scope, but you can't assign to or read from them until they've been declared. I know it's a little odd, but in practice it's rarely a problem. (It doesn't have scope for the whole method, btw, only the whole block it's declared in.) -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 15 '05 #17

 P: n/a Champika Nirosh wrote: http://msdn.microsoft.com/library/de...us/vbcn7/html/ vbconscopelevels.asp That's about VB though, which may well (I haven't checked) have entirely different rules to C#, which is the language under discussion. -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 15 '05 #18

 P: n/a No I don't think since it is .Net. Rules that are use in CLR are almost same for both C# and VB. "Jon Skeet [C# MVP]" wrote in message news:MP************************@msnews.microsoft.c om... Champika Nirosh wrote: http://msdn.microsoft.com/library/de...us/vbcn7/html/ vbconscopelevels.asp That's about VB though, which may well (I haven't checked) have entirely different rules to C#, which is the language under discussion. -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 15 '05 #20