Local variables - quick question

  • Thread starter Stefan Turalski \(stic\)
  • Start date
S

Stefan Turalski \(stic\)

Hi,

I done sth like this:

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

and after this local declaration of i variable I try to inicialize

int i=0;

And VS.NET 2003 gives me error:
" 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"

Anyone could explain me this is - how can I rewrite this ?
 
J

Jon Skeet [C# MVP]

Stefan Turalski (stic) said:
I done sth like this:

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

and after this local declaration of i variable I try to inicialize

int i=0;

And VS.NET 2003 gives me error:
" 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"

Anyone could explain me this is - how can I rewrite this ?

Use different variable names, basically. The scope of a local variable
is the whole block it's declared in, and you can't declare a variable
with a name which is already "taken" within that scope.

Alternatively, you could possibly start a new scope, depending on
exactly what you were doing:

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

{
int i = 5;
//
}

would be okay.
 
S

Stefan Turalski \(stic\)

Uzytkownik "Jon Skeet said:
Use different variable names, basically. The scope of a local variable
is the whole block it's declared in, and you can't declare a variable
with a name which is already "taken" within that scope.

Alternatively, you could possibly start a new scope, depending on
exactly what you were doing:

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

{
int i = 5;
//
}

would be okay.

Hi,

Thanks for your quick answer - all what I did was to rename this variable.
btw. Is there a way to read ahead variable scope? Or where msdn gives info
about scope of variables - I wasn't expecting that local for() - loop have
scope over this loop scope ;-)
 
J

Jon Skeet [C# MVP]

Stefan Turalski (stic) said:
Thanks for your quick answer - all what I did was to rename this variable.
btw. Is there a way to read ahead variable scope?

What exactly do you mean by "read ahead variable scope"?
Or where msdn gives info
about scope of variables - I wasn't expecting that local for() - loop have
scope over this loop scope ;-)

Look at section 10.7 of the ECMA C# spec
(http://www.jaggersoft.com/csharp_standard/10.7.htm)
or section 3.7 of the MS spec
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/csspec/html/vclrfcsharpspec_3_7.asp,
aka http://tinyurl.com/394xc
 
S

Stefan Turalski \(stic\)

Uzytkownik "Jon Skeet said:
What exactly do you mean by "read ahead variable scope"?

What should I do if I want to examine one of my variables if it is in its
scope or no ?
Debbuger could do it in some why ?

Variables declared localy as far as I know should be destroyed by GC after
their scope ends.. I'm I right ? (rather not, but I have no idea what the
answer is)
Look at section 10.7 of the ECMA C# spec
(http://www.jaggersoft.com/csharp_standard/10.7.htm)

- great link! thanks a lot
or section 3.7 of the MS spec
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/csspec/html/vclrfcsharpspec_3_7.asp,
aka http://tinyurl.com/394xc

- another good link (this one I have reed before writing on group - and then
I was wondering what:
"Scopes can be nested, and an inner scope may redeclare the meaning of a
name from an outer scope."
mean ? I asume that when I write another for() loop with the same int i
iterator I only redeclare it ?

What is the dipper (memory?) backgroud of this 'redeclare' ?

Sorry for all this questions but I need to know !!!

And once again, thanks for your help...
 
J

Jon Skeet [C# MVP]

Stefan Turalski (stic) said:
What should I do if I want to examine one of my variables if it is in its
scope or no ?

In what context? You can't use a variable which isn't in scope within
code.
Debbuger could do it in some why ?

I don't believe even the debugger will show you a variable out of its
scope.
Variables declared localy as far as I know should be destroyed by GC after
their scope ends.. I'm I right ? (rather not, but I have no idea what the
answer is)

Variables aren't "destroyed", but they will no longer be treated as
being alive by the garbage collector. In other words, just because a
variable comes to the end of its scope doesn't mean that an object it
refers to will immediately be garbage collected, but it means that the
variable will no longer stop if from being garbage collected. (In fact,
when not in debug mode, it's actually after the last use that it's no
longer "used".)
- another good link (this one I have reed before writing on group - and then
I was wondering what:
"Scopes can be nested, and an inner scope may redeclare the meaning of a
name from an outer scope."

It means you can have, say, a local variable with the same name as an
instance variable.
mean ? I asume that when I write another for() loop with the same int i
iterator I only redeclare it ?

No - you should read onto the next sentence:

<quote>
(This does not, however, remove the restriction imposed by Section 3.3
that within a nested block it is not possible to declare a local
variable with the same name as a local variable in an enclosing block.)
What is the dipper (memory?) backgroud of this 'redeclare' ?

Not sure what you're after here. Names are just names - by the time
everything's compiled there may still be a mapping of name to (eg)
member location or place in the stack for a local variable, but unless
you're using reflection, the runtime itself won't be using that map
much.
 
S

Stefan Turalski \(stic\)

Uzytkownik "Jon Skeet said:
In what context? You can't use a variable which isn't in scope within
code.

Yes, that is why i can't write:
for(int i=0;i<10;i++){}
Console.Write("{0}",i);

And this I undestood well, but what if I want to check if there is
possiblity to declare class argument int i - only signal for me is compiler
errors ?
I really try but I don't get it (yet ;) why compiler don't allow me to use
'child' scope variable on this level ?
for(int i=0;i<10;i++){}
int i=0;

This is as you said variable which isn't in scope - 'child' scope is this
for-loop scope..
I don't believe even the debugger will show you a variable out of its
scope.
But there have to be a way to go over this and see what is happening with
this all 'non-used' objects ? I have to go over garbage collector ?
Variables aren't "destroyed", but they will no longer be treated as
being alive by the garbage collector. In other words, just because a
variable comes to the end of its scope doesn't mean that an object it
refers to will immediately be garbage collected, but it means that the
variable will no longer stop if from being garbage collected. (In fact,
when not in debug mode, it's actually after the last use that it's no
longer "used".)

So when I still try to do something like for-loop in which I use i =
iterator, and after that I would like to use int i - new declaration. That
could not be done, becouse of this obcjet already could be somewhere in
memory ? I don't get this idea of "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"

It means you can have, say, a local variable with the same name as an
instance variable.

Ok, so why my int i; isn't instance variable ??
No - you should read onto the next sentence:
<quote>
(This does not, however, remove the restriction imposed by Section 3.3
that within a nested block it is not possible to declare a local
variable with the same name as a local variable in an enclosing block.)
</quote>

I was thinking about something like this
for(int i=0;i<10;i++){ do sth A..}
for(int i=10;i>0;i--){ do sth B..}

And this works well, both 'child' scopes are using the same name for
variable and there is no errors, so another time I ask -> why class variable
(I think instance ?) isn't allowed as well ?
Not sure what you're after here. Names are just names - by the time
everything's compiled there may still be a mapping of name to (eg)
member location or place in the stack for a local variable, but unless
you're using reflection, the runtime itself won't be using that map
much.
I was wondering how my previous thing works, if I have two declarations of
int i - there must be a way to forget about this first name and make another
object for second 'i' - and why the same thing couldn't be done for int i -
not in local for-loop scope but at class level.

I really try to understood this... really..
 
J

Jon Skeet [C# MVP]

Stefan Turalski (stic) said:
Yes, that is why i can't write:
for(int i=0;i<10;i++){}
Console.Write("{0}",i);

And this I undestood well, but what if I want to check if there is
possiblity to declare class argument int i - only signal for me is compiler
errors ?

You could declare an instance variable i, yes.
I really try but I don't get it (yet ;) why compiler don't allow me to use
'child' scope variable on this level ?
for(int i=0;i<10;i++){}
int i=0;

This is as you said variable which isn't in scope - 'child' scope is this
for-loop scope..

If you have a local variable hiding an instance variable, you can
always get to the instance variable using this.i. You wouldn't be able
to get to the "outer scope" for another local variable. In addition, I
suspect the language designers considered it would make code less
readable in general.
scope.
But there have to be a way to go over this and see what is happening with
this all 'non-used' objects ? I have to go over garbage collector ?

I'm really not sure what you're trying to do, to be honest. Please give
a full description of what you're trying to do and in what context.
So when I still try to do something like for-loop in which I use i =
iterator, and after that I would like to use int i - new declaration. That
could not be done, becouse of this obcjet already could be somewhere in
memory ? I don't get this idea of "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"

No, it's nothing to do with objects being in memory. It's just that the
language is defined to prevent local variables from hiding each other.
Ok, so why my int i; isn't instance variable ??

Because it's a local variable. An instance variable is one which
belongs to the instance; a local variable is one which belongs to the
method.
I was thinking about something like this
for(int i=0;i<10;i++){ do sth A..}
for(int i=10;i>0;i--){ do sth B..}

And this works well, both 'child' scopes are using the same name for
variable and there is no errors, so another time I ask -> why class variable
(I think instance ?) isn't allowed as well ?

It is. You just can't have another *local* variable called i in the
scope which includes those two loops.
I was wondering how my previous thing works, if I have two declarations of
int i - there must be a way to forget about this first name and make another
object for second 'i' - and why the same thing couldn't be done for int i -
not in local for-loop scope but at class level.

I really try to understood this... really..

No objects are involved here in the first place, as we're dealing with
integers.

You'll have to accept that you just can't have two local variables
declared where one is declared in either the same scope or the child
scope of the other.
 
S

Stefan Turalski \(stic\)

Uzytkownik "Jon Skeet said:
If you have a local variable hiding an instance variable, you can
always get to the instance variable using this.i. You wouldn't be able
to get to the "outer scope" for another local variable. In addition, I
suspect the language designers considered it would make code less
readable in general.

Ok, so I'll remember this as the way this was done... ;-)
I'm really not sure what you're trying to do, to be honest. Please give
a full description of what you're trying to do and in what context.

In meantime of digging in scope's documentation I was wondering if there is
tool which alows me to monitor which objects and what more important
instances of local variables are 'used', and which could be free by GC. When
I didn't find such tool (especially for int instances) - I was wonder of my
own tool for this. That's why I asked about it, becouse I even don't know if
its possible.
No, it's nothing to do with objects being in memory. It's just that the
language is defined to prevent local variables from hiding each other.
Oh, now I get it ;-) thanks...
Because it's a local variable. An instance variable is one which
belongs to the instance; a local variable is one which belongs to the
method.
That's what I need ! ;-) And what I don't understood - when this is a local,
and why ? now I know... You are my guru ;-)
Sometimes like a horse (or maybe a donkey ;) I have blanked eyes...
You'll have to accept that you just can't have two local variables
declared where one is declared in either the same scope or the child
scope of the other.

I do it !
Thanks - I owe you a pint... ;-)
 
J

Jon Skeet [C# MVP]

Stefan Turalski (stic) said:
In meantime of digging in scope's documentation I was wondering if there is
tool which alows me to monitor which objects and what more important
instances of local variables are 'used', and which could be free by GC. When
I didn't find such tool (especially for int instances) - I was wonder of my
own tool for this. That's why I asked about it, becouse I even don't know if
its possible.

No, I don't believe there is such a tool. I can't remember whether the
JIT works out when a variable is last used, or whether that information
is stored in the assembly. It's not usually very useful though, IMO.
That's what I need ! ;-) And what I don't understood - when this is a local,
and why ? now I know... You are my guru ;-)
Sometimes like a horse (or maybe a donkey ;) I have blanked eyes...

It's often the way - a single moment of insight, and everything makes
sense. It happens to all of us, so don't worry about it at all :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Possible Compiler Bug (C# 3.0) 10
Reusing the name of local variable 19
simple example 14
why do I get compile error 2
Variable vs. child scopes 3
foreach help 2
Nonplussed by scoping - any explanations? 2
Syntax error 2

Top