scope question :

  • Thread starter Thread starter NotYetaNurd
  • Start date Start date
yes you go ... that is only from the things you have define within the scope
of the for loop

Nirosh.
 
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
 
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'
 
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 said:
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(....)
{
....
}
 
Champika Nirosh said:
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);
}
}
}
}
 
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.
 
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......."
 
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.
 
NotYetaNurd said:
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.
 
Champika Nirosh said:
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.)
 
Champika Nirosh said:
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.)
 
Champika Nirosh said:
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>
 
No I don't think since it is .Net. Rules that are use in CLR are almost same
for both C# and VB.
 
Back
Top