By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,198 Members | 925 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
23 Replies


P: n/a
Yep.

"NotYetaNurd" <No*********@Matrix.com> 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" <No*********@Matrix.com> 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<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"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
"NotYetaNurd" <No*********@Matrix.com> 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" <No*********@Matrix.com> wrote in message
news:#1**************@TK2MSFTNGP10.phx.gbl...
when i tried this
for(int i=0;i<6;i++)
{
for(int j=0;j<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"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
"NotYetaNurd" <No*********@Matrix.com> 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" <no****@textcentric.lk> 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" <No*********@Matrix.com> wrote in message
news:#1**************@TK2MSFTNGP10.phx.gbl...
when i tried this
for(int i=0;i<6;i++)
{
for(int j=0;j<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"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
"NotYetaNurd" <No*********@Matrix.com> 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" <No*********@Matrix.com> 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" <no****@textcentric.lk> 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" <No*********@Matrix.com> wrote in message
news:#1**************@TK2MSFTNGP10.phx.gbl...
when i tried this
for(int i=0;i<6;i++)
{
for(int j=0;j<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"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
"NotYetaNurd" <No*********@Matrix.com> 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 <No*********@Matrix.com> wrote:
when i tried this
for(int i=0;i<6;i++)
{
for(int j=0;j<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"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


Indeed. The problem is that the "int i=1;" declaration has a scope for
its entire block - including the for loop which occurs before it.
Effectively, you've got:

int i;

for (int i=0; i < 6; i++)
{
....
}

i=1;
foreach(....)
{
....
}

--
Jon Skeet - <sk***@pobox.com>
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 <no****@textcentric.lk> 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 - <sk***@pobox.com>
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 <no****@textcentric.lk> 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 - <sk***@pobox.com>
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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Champika Nirosh <no****@textcentric.lk> 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 - <sk***@pobox.com>
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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
NotYetaNurd <No*********@Matrix.com> wrote:
when i tried this
for(int i=0;i<6;i++)
{
for(int j=0;j<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"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


Indeed. The problem is that the "int i=1;" declaration has a scope for
its entire block - including the for loop which occurs before it.
Effectively, you've got:

int i;

for (int i=0; i < 6; i++)
{
...
}

i=1;
foreach(....)
{
...
}

--
Jon Skeet - <sk***@pobox.com>
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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
NotYetaNurd <No*********@Matrix.com> wrote:
when i tried this
for(int i=0;i<6;i++)
{
for(int j=0;j<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"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


Indeed. The problem is that the "int i=1;" declaration has a scope for
its entire block - including the for loop which occurs before it.
Effectively, you've got:

int i;

for (int i=0; i < 6; i++)
{
...
}

i=1;
foreach(....)
{
...
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #13

P: n/a
NotYetaNurd <No*********@Matrix.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #14

P: n/a
Champika Nirosh <no****@textcentric.lk> wrote:
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.


So what *exactly* do you mean by lifetime here? (I had thought you'd
just changed terminology, as the rest of the thread is about scope and
your explanation didn't really explain muhc, IMO.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #15

P: n/a
Please Read

http://msdn.microsoft.com/library/de...us/vbcn7/html/
vbconscopelevels.asp

Nirosh

"Champika Nirosh" <no****@textcentric.lk> 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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Champika Nirosh <no****@textcentric.lk> 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 - <sk***@pobox.com>
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 <no****@textcentric.lk> 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 - <sk***@pobox.com>
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 <no****@textcentric.lk> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #18

P: n/a
Champika Nirosh <no****@textcentric.lk> wrote:
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.


Okay, I think it's time to start looking at what the spec has to say.
ECMA numbering used throughout.

From section 12.1.7:

<quote>
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.
</quote>

So no, not the whole method - just the block it's associated with.

Also from 12.1.7:

<quote>
Within the scope of a local variable, it is a compile-time error to
refer to that local variable in a textual position that precedes its
local-variable-declarator.
</quote>

That addresses your other question about why you can't do

i=1;
int i;

From section 10.7:

<quote>
The scope of a local variable declared in a local-variable-declaration
(15.5.1) is the block in which the declaration occurs.
The scope of a local variable declared in a switch-block of a switch
statement (15.7.2) is the switch-block.
The scope of a local variable declared in a for-initializer of a for
statement (15.8.3) is the for-initializer, the for-condition, the for-
iterator, and the contained statement of the for statement.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #19

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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Champika Nirosh <no****@textcentric.lk> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #20

P: n/a
Agreed, no argument

Can you just read the link I gave and made your comments?

Nirosh.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Champika Nirosh <no****@textcentric.lk> wrote:
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.


Okay, I think it's time to start looking at what the spec has to say.
ECMA numbering used throughout.

From section 12.1.7:

<quote>
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.
</quote>

So no, not the whole method - just the block it's associated with.

Also from 12.1.7:

<quote>
Within the scope of a local variable, it is a compile-time error to
refer to that local variable in a textual position that precedes its
local-variable-declarator.
</quote>

That addresses your other question about why you can't do

i=1;
int i;

From section 10.7:

<quote>
The scope of a local variable declared in a local-variable-declaration
(15.5.1) is the block in which the declaration occurs.
The scope of a local variable declared in a switch-block of a switch
statement (15.7.2) is the switch-block.
The scope of a local variable declared in a for-initializer of a for
statement (15.8.3) is the for-initializer, the for-condition, the for-
iterator, and the contained statement of the for statement.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #21

P: n/a
Champika Nirosh <no****@textcentric.lk> wrote:
No I don't think since it is .Net. Rules that are use in CLR are almost same
for both C# and VB.


Rules of scope for a variable are *language* rules, not CLR rules.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #22

P: n/a
Champika Nirosh <no****@textcentric.lk> wrote:
Agreed, no argument

Can you just read the link I gave and made your comments?


I'm not sure I see the point, to be honest - that's VB.NET, which could
have entirely different scoping rules. Just because they both target
..NET doesn't mean things have to work exactly the same way.

For instance, you could have a language D# which *did* allow

i=5;
int i;

Why would that have to change what code is generated? The D# compiler
could just rearrange the variable declaration to the start of its
scope, and then call the C# compiler. They would both target .NET, but
would have different rules for how variables could be accessed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #23

P: n/a
Champika,
for (int i=0; i<6; i++) === int i; for(i=0; i<6; i+) That doesn't sound correct, as I can do:

for(int i=0;i<6;i++)
{

}
for(long i=0;i<6;i++)
{

}

Which does compile & run.

By your statement the following would be equal, which actually causes a
compile error!

int i; for(i=0;i<6;i++)
{

}
long i; for(i=0;i<6;i++)
{

}

As i is already defined to be an int!

I will defer to one of the C# experts as to the "why"!

Hope this helps
Jay

"Champika Nirosh" <no****@textcentric.lk> 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" <No*********@Matrix.com> wrote in message
news:#1**************@TK2MSFTNGP10.phx.gbl...
when i tried this
for(int i=0;i<6;i++)
{
for(int j=0;j<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"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
"NotYetaNurd" <No*********@Matrix.com> 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 #24

This discussion thread is closed

Replies have been disabled for this discussion.