473,837 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Local variables - quick question

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 ?

--
best regards
stic
Nov 15 '05 #1
9 2213
Stefan Turalski (stic) <st************ *@kruk-inkaso.com.pl> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Uzytkownik "Jon Skeet [C# MVP]" <sk***@pobox.co m> napisal w wiadomosci
news:MP******** *************** *@msnews.micros oft.com...
Stefan Turalski (stic) <st************ *@kruk-inkaso.com.pl> wrote:
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.

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


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 ;-)

--
best regards
stic


Nov 15 '05 #3
Stefan Turalski (stic) <st************ *@kruk-inkaso.com.pl> wrote:
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/de...l=/library/en-
us/csspec/html/vclrfcsharpspec _3_7.asp,
aka http://tinyurl.com/394xc

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Uzytkownik "Jon Skeet [C# MVP]" <sk***@pobox.co m> napisal w wiadomosci
news:MP******** *************** *@msnews.micros oft.com...
Stefan Turalski (stic) <st************ *@kruk-inkaso.com.pl> wrote:
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"?


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)
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)


- great link! thanks a lot
or section 3.7 of the MS spec
http://msdn.microsoft.com/library/de...l=/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...

--
best regards
Stic
Nov 15 '05 #5
Stefan Turalski (stic) <st************ *@kruk-inkaso.com.pl> wrote:
btw. Is there a way to read ahead variable scope? 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 ?


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

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

Uzytkownik "Jon Skeet [C# MVP]" <sk***@pobox.co m> napisal w wiadomosci
news:MP******** *************** *@msnews.micros oft.com...
Stefan Turalski (stic) <st************ *@kruk-inkaso.com.pl> wrote:
> btw. Is there a way to read ahead variable scope?
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 ?

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..
Debbuger could do it in some why ?

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 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".)


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"

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


Ok, so why my int i; isn't 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.)
</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 ?
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.

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

--
greatly thanks for your time
stic
Nov 15 '05 #7
Stefan Turalski (stic) <st************ *@kruk-inkaso.com.pl> wrote:
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 ?


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.
Debbuger could do it in some why ?

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 ?


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.
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"


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


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.
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.)
</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 ?


It is. You just can't have another *local* variable called i in the
scope which includes those two loops.
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.

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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
Uzytkownik "Jon Skeet [C# MVP]" <sk***@pobox.co m> napisal w wiadomosci
news:MP******** *************** *@msnews.micros oft.com...
Stefan Turalski (stic) <st************ *@kruk-inkaso.com.pl> wrote: [..]
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.


Ok, so I'll remember this as the way this was done... ;-)
> Debbuger could do it in some why ?
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 ?

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

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... ;-)

--
best regards
stic

Nov 15 '05 #9
Stefan Turalski (stic) <st************ *@kruk-inkaso.com.pl> wrote:
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.
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.

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 :)

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2892
by: Tim Hill | last post by:
I'm just getting up to speed on Java, and have a question about local class behavior. Here's a mini-app to illustrate: interface Foo { void print(); } class LocalClassTest { public static void main(String args) { Foo foo = getFooImpl(100); foo.print();
4
2031
by: Rigs | last post by:
Hi, Does anyone happen to have a debug script that will display the variable name and value for all of the local variables on an .asp page? (not session nor application, as I already have scripts those.) I'm looking for something that can be included in an .asp to write out values for debugging. Does anyone know what scope those variables reside in? Please help if you can.
9
2532
by: cppaddict | last post by:
I have a method that uses a fairly large object. The choice is between having a local object in the method or a static member object that the method uses. ------CHOICE 1--------- int MyClass::myMethod(int param) { MyBigObject o(); //initialize o based on param //do calculations with o
7
3160
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my question about local variable initialization is still not solved. And some of the replies forked into talking about out parameters. And the thread is becoming way too deep. So I open a new thread here. My question in the previous thead has turned...
3
1606
by: Robin Tucker | last post by:
Should I consider this to work in other locales? If theServer.Name <> "(local)" and theServer.Name <> "(localhost)" Then ' Can only use local machine in this version..... End If
4
3002
by: Marc E | last post by:
All, I'm coming from java and coldfusion, where one can set a "global" variable in one place (the servletcontext in java, Application.cfm in coldfusion) and all files in that site can then take advantage of these variables without the need to "include" a variables page in every page on the site. Is there something comparable in php, like an Application.php or some such thing? One more question that's basically an extension of the first....
55
6276
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
1
1714
by: danep2 | last post by:
Let me start by saying that this is more a question about principle than practice - with the speed of today's computers it's probably rarely an actual issue. Still I'd like to know... If I have a function that is called thousands of times per second, it seems to me that, performance-wise, it would be best to make all variables used in it global, so that memory for them doesn't have to be allocated and released with each call. However,...
8
1942
by: Samant.Trupti | last post by:
Hi All, I am facing a strange problem.. I am calling a function func2 from func1. Before calling func2 all the local variables in func1 looks fine and has their respective values. When function returns back from func2 to func1 all the local variables in func1 has values 0xfdfdfd. Can somebody please explain me what is happening and what is the solution for this? Thanks
0
9679
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10611
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10263
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9390
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6988
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4034
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3120
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.