TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Jitse
NA
8
0
Local variable cannot be declared because it would give a different meaning to the variable in a 'child' scope... Huh?
Nov 17 2007 11:28 AM
Hi ho,
I've encountered a weird thing with C#. Apparently when you do something like this:
private void MyMethod()
{
for (int i = 0; i < 10; i++)
{
}
int i = 5; // this gives an error...
}
This is just extremely weird and annoying. According to MS this is to protect the programmer, for example when he copy/paste a piece of code. But that's just ridiculous, a variable declared in the scope of the for loop should stay there, and get disposed when the loop ends. And even weirder is that you can still do this:
private void MyMethod()
{
for (int i = 0; i < 10; i++)
{
}
for (int i = 0; i < 10; i++) // shouldn't this give an error then? no it doesn't, don't ask me why...
{
}
}
Why does this compile then? If MS wants i to have the scope of the whole method, why am I still able then to declare it in a second for loop, but not in a normal way?
Thanks in advance,
Jitse
Reply
Answers (
4
)
DataGrid TextBox does not accept Double values
What are some good beginner programmes I could write to practice?