473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Nonplussed by scoping - any explanations?

Hi-

I am confused by C#'s scoping of variables within a child scope (for
loop, conditional, block of code), and was hoping someone could
explain. Here's the offending code:

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

The compiler complains:
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

This doesn't make sense. The variable i defined in the for loop
should go out of scope after the closing curly brace, and it should be
OK to define a new variable which just happens to have the same name,
right?

If i is still defined after the for block, this should work fine:

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

i = 100;

But this doesn't work either:
The name 'i' does ot exist in the class or namespace 'Dooku.kudu.bobo'

So it looks to me like i is neither in scope or out of scope after the
for loop. It's in limbo!

Any ideas?

Thanks,
Diane.

P.S. THis is not just a quirk of the for loop. THe behavior is the
same with conditionals and just plain old {} blocks...
Nov 16 '05 #1
2 917
"Diane Selby" <so*************@yahoo.com> wrote in message
news:29**************************@posting.google.c om...
Hi-

I am confused by C#'s scoping of variables within a child scope (for
loop, conditional, block of code), and was hoping someone could
explain. Here's the offending code:

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

<snip>

Diane,

The reason this doesn't work is because although C# lets you define a
variable anywhere within a block, its scope still applies to the whole
block. It may APPEAR that you've defined the variable halfway through the
block, and the scope only applies from that line forward, but really it got
allocated on the stack as soon as you entered the block. In standard C, it
was enforced that you always had to declare all your variables at the
beginning of a block. Personally I prefer the standard C way.

But keep in mind that even though variable declarations apply to the entire
block, the compiler will squawk at you if you try to do this:

{
i = 10;
int i;
}

As a style guideline, if you follow the practice of always declaring your
variables at the beginning of your code blocks, you'll never run into
problems like the one you've stated.

Erik
Nov 16 '05 #2
Diane Selby <so*************@yahoo.com> wrote:
I am confused by C#'s scoping of variables within a child scope (for
loop, conditional, block of code), and was hoping someone could
explain. Here's the offending code:

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

The compiler complains:
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

This doesn't make sense. The variable i defined in the for loop
should go out of scope after the closing curly brace, and it should be
OK to define a new variable which just happens to have the same name,
right?


Wrong.

From the C# spec:

<quote>
The scope of a local variable declared in a local-variable-declaration
(§15.5.1) is the block in which the declaration occurs.
</quote>

In other words, your second variable "i" is in *scope* (although not
actually available for use) *before* the for loop.

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

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

Similar topics

8
by: Ian McMeans | last post by:
I was bitten by a bug today that depended on how lambda works. It took me quite a while to realize what was going on. First, I made multiple lambda functions inside a loop, each of which...
2
by: David Stockwell | last post by:
Hi, Another of my crazy questions. I'm just in the process of learning so bear with me if you can. I actually ran it.... with two test cases TEST CASE 1: Say I have the following defined:...
2
by: Robert M. Gary | last post by:
I'm curious what the ANSI C++ standard says about nested classes. I'm not able to find where in the ANSI C++ standard this is addressed. The issue is the accessibility of sibling nested classes....
1
by: Michael | last post by:
I am having a problem with scoping of parameters in my XSLT Stylesheet...here is the stylesheet (the xml document is irrelevant for the example) <?xml version="1.0" encoding="UTF-8"?>...
2
by: Diane Selby | last post by:
Hi- I am confused by C#'s scoping of variables within a child scope (for loop, conditional, block of code), and was hoping someone could explain. Here's the offending code: for (int i=0;...
4
by: Joel Gordon | last post by:
Hi, When I try and compile the a class containing the following method : public void doSomething() { for (int i=0; i<5; i++) { IList list = new ArrayList(); Console.WriteLine( i /...
9
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13,...
3
by: morris.slutsky | last post by:
So every now and then I like to mess around with hobby projects - I often end up trying to write an OpenGL video game. My last attempt aborted due to the difficulty of automating game elements and...
17
by: Chad | last post by:
The following question stems from Static vs Dynamic scoping article in wikipedia. http://en.wikipedia.org/wiki/Scope_(programming)#Static_versus_dynamic_scoping Using this sites example, if I...
3
by: SPECTACULAR | last post by:
Hi all. I have a question here.. what kind of scoping does C++ use? and what kind does Smalltalk use? I know smalltalk is a little bit old .. but any help would be appreciated.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.