473,327 Members | 2,025 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,327 software developers and data experts.

why is this illegal

this fragment of c# does not compile:

{
{
string s = "a";
}
string s = "a";
}

It says that the 2 declarations conflict. AFAIK the scope of a variable
declaration is from the point in the source file it is declared until the
end of the enclosing block. By this definition they don't conflict

Andy
Nov 17 '05 #1
5 1088
Why would you think this is the case?

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx

"Andy Fish" <aj****@blueyonder.co.uk> wrote in message
news:uj**************@TK2MSFTNGP10.phx.gbl...
this fragment of c# does not compile:

{
{
string s = "a";
}
string s = "a";
}

It says that the 2 declarations conflict. AFAIK the scope of a variable
declaration is from the point in the source file it is declared until the
end of the enclosing block. By this definition they don't conflict

Andy

Nov 17 '05 #2
Isn't it true? For example:

static void myMethod()
{
for(int iCount = 0; iCOunt <= 100; iCount++)
{
Console.WriteLine("Value of iCount: " + iCount.ToString());
}

// fails because iCount is declared in the for loop, and the variable
falls out of scope
// after the for loop ends...
Console.WriteLine("Value of iCount: " + iCount.ToString());
}
"Robbe Morris [C# MVP]" <in**@turnkeytools.com> wrote in message
news:Oc**************@TK2MSFTNGP12.phx.gbl...
Why would you think this is the case?

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx

"Andy Fish" <aj****@blueyonder.co.uk> wrote in message
news:uj**************@TK2MSFTNGP10.phx.gbl...
this fragment of c# does not compile:

{
{
string s = "a";
}
string s = "a";
}

It says that the 2 declarations conflict. AFAIK the scope of a variable
declaration is from the point in the source file it is declared until the
end of the enclosing block. By this definition they don't conflict

Andy


Nov 17 '05 #3
Technically, they do not conflict. However, 99.9% of the time, when you
have something like that, it's a mistake, and you accidentally refer to "s"
thinking you're getting the other one.

C++ lets you shot yourself inthe foot that way. C# does not.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Andy Fish" <aj****@blueyonder.co.uk> wrote in message
news:uj**************@TK2MSFTNGP10.phx.gbl...
this fragment of c# does not compile:

{
{
string s = "a";
}
string s = "a";
}

It says that the 2 declarations conflict. AFAIK the scope of a variable
declaration is from the point in the source file it is declared until the
end of the enclosing block. By this definition they don't conflict

Andy

Nov 17 '05 #4
James Curran <ja*********@mvps.org> wrote:
Technically, they do not conflict. However, 99.9% of the time, when you
have something like that, it's a mistake, and you accidentally refer to "s"
thinking you're getting the other one.

C++ lets you shot yourself inthe foot that way. C# does not.


No, they *do* conflict according to the spec.

From section 10.7 of the 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>

It's the whole block, not "from the point of declaration onwards".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
why it's illegal?
if ypu want to know how's the rule:
The scope of a local variable declared in a local-variable-declaration is
the block in which the declaration occurs.
But:
It is an error to refer to a local variable in a textual position that
precedes the local-variable-declarator of the local variable.

if you want to know why c# does this?
See posting of James Curran

Andy Fish" <aj****@blueyonder.co.uk> schrieb im Newsbeitrag
news:uj**************@TK2MSFTNGP10.phx.gbl...
this fragment of c# does not compile:

{
{
string s = "a";
}
string s = "a";
}

It says that the 2 declarations conflict. AFAIK the scope of a variable
declaration is from the point in the source file it is declared until the
end of the enclosing block. By this definition they don't conflict

Andy

Nov 17 '05 #6

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

Similar topics

2
by: Frances Del Rio | last post by:
Pls, what are all illegal chars in e-mail addreses? specif. I'd like to know if apostrophe is allowed, but would be handy to find a list of all illegal chars for e-mail addresses.. searched...
0
by: MB | last post by:
Hi, I am using a web service to call an asp page on a remote server via System.Net.WebClient . The asp page returns a xml stream persisted from a recordset. I am having problems converting the...
14
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal =...
6
by: Skybuck Flying | last post by:
unsigned long long int __a; /* Factor in congruential formula. */ C:\C TESTEN\TestNRand48\Main.cpp(29) : error C2632: 'long' followed by 'long' is illegal ???
5
by: Dave | last post by:
I need to cut out illegal characters in a string submitted from a mobile phone to a web form. I need a way to check for the illegal characters in a textbox. I intend to loop through the text and...
0
by: Robin Munn | last post by:
I'm developing a simple proof-of-concept Web application, more as a personal programming exercise than anything else, that presents the user with a login form where they can type in a database...
2
by: Gabrielle A. Grün | last post by:
Hi All, Does anyone know a way around the illegal reference of a non-static member? Thanks. iNHERITANCE HIERACY
6
by: BlueTrin | last post by:
Hello I was adapting a C version of SolvOpt in C++ to use it within a virtual class. However I am stuck with the overriding of evaluation and gradiant functions. cStepCurveEvaluator.cpp...
5
by: Roy Smith | last post by:
The following code appears to be illegal: while ((int c = getchar()) != EOF) { putchar (c); } I tried it on two different compilers (Sun workshop and gcc), and both give some variation on...
4
by: Mathias Waack | last post by:
Hi, I've embedded python into a legacy application. It works - most of the time. In some special situations the app crashes executing the "import random". There are two different situations: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.