Hey CG, C# is pretty weird too
-
using System;
class B
{
public static void Main()
{
if(true)
{
B b = new B();
}B b = new B(); // <-- throws error CS0136 }
}
error CS0136: A local variable named 'b' cannot be declared in this scope because it would give a different meaning to 'b', which is already used in a 'child' scope to denote something else :wtf: :wtf: :wtf:
My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com
Could it be that if(true) is being optimized out by the compiler? What happens if true is replaced by a non-const variable? Kind regards, Tim
-
Alright, I can see how this is probably an attempt to enforce good coding/naming practices, but it should only have been a warning. Throwing a compiler error is outright dumb! Nish
My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com
Why warnings - a real coder always treats warnings as errors - right ? Richard "Under certain circumstances, profanity provides a relief denied even to prayer --Mark Twain (1835 - 1910)
-
Uh, the "throws error" really got me confused there for a minute, thinking that the runtime threw an exception. As compiler errors go, this one is both annoying, coming from C++, and has saved my butt several times. One thing that I thought was interesting that I didn't see anyone mentioning in the VB.NET vs. C# threads is that C# is a standard language while VB.NET is basically just whatever Microsoft wants it to be (this is true, right?). As a result, C# is being ported to other platforms. Is VB.NET getting generics? Are there any open source VB.NET projectscompilers? I haven't heard of any developments along these lines. Just one more comment--there are a couple VB.NET to C# converters out there. I haven't seen any C# to VB.NET converters. Why is that? (Besides that I haven't bothered actually looking). Marc MyXaml Advanced Unit Testing
Marc Clifton wrote: Are there any open source VB.NET projectscompilers? Yes, MonoBasic[^].
Any remotely useful information on my blog will be removed immediately.
There are 10 kinds of people in the world. Those who have heard of the ubiquitous, overused, worn-out-like-an-old-shoe binary "joke" and those who haven't. Judah Himango
-
Dont be so lazy!
using System;
class B
{
public static void Main()
{
if(true)
{
B b = new B();
}
{ B b = new B(); } // <-- throws no error CS0136
}
}top secret
Download xacc-ide 0.0.3 now!
See some screenshotsAccording to section 3.3 of the C# Language Specification "The local variable declaration space of a block includes any nested blocks." That's why Nish's code didn't compile. Your code declares instances of B in two separate blocks, so it compiles fine.
-
using System;
class B
{
public static void Main()
{
if(true)
{
B b = new B();
}B b = new B(); // <-- throws error CS0136 }
}
error CS0136: A local variable named 'b' cannot be declared in this scope because it would give a different meaning to 'b', which is already used in a 'child' scope to denote something else :wtf: :wtf: :wtf:
My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com
I noticed that one too. Fascist compiler. :mad: -- Weiter, weiter, ins verderben. Wir müssen leben bis wir sterben. I blog too now[^]
-
Could it be that if(true) is being optimized out by the compiler? What happens if true is replaced by a non-const variable? Kind regards, Tim
The symbol tables are built far earlier in the compiling process than optimization. In fact, optimization of some piece of code cannot occur until all semantic question marks pertaining to that code have been resolved. -- Weiter, weiter, ins verderben. Wir müssen leben bis wir sterben. I blog too now[^]
-
According to section 3.3 of the C# Language Specification "The local variable declaration space of a block includes any nested blocks." That's why Nish's code didn't compile. Your code declares instances of B in two separate blocks, so it compiles fine.
That's just silly since you cannot access variables declared in a nested scope. It's counter intuitive for, dare I say, all programmers. Name one language which has similar scoping rules? -- Weiter, weiter, ins verderben. Wir müssen leben bis wir sterben. I blog too now[^]
-
That's just silly since you cannot access variables declared in a nested scope. It's counter intuitive for, dare I say, all programmers. Name one language which has similar scoping rules? -- Weiter, weiter, ins verderben. Wir müssen leben bis wir sterben. I blog too now[^]
It's silly if you look at it that way. But this does help in some cases (see Marc's post above) where it prevents redeclaration of a variable with a same name in a nested scope. I've done that quite a few times and spent a considerable time debugging the problem. Regards Senthil
-
It's silly if you look at it that way. But this does help in some cases (see Marc's post above) where it prevents redeclaration of a variable with a same name in a nested scope. I've done that quite a few times and spent a considerable time debugging the problem. Regards Senthil
S. Senthil Kumar wrote: where it prevents redeclaration of a variable with a same name in a nested scope Yes, in the "forward" direction I can see the point. But it also blocks in the "backward" direction, which is silly since the outer/after scope cannot reference the variable declared in the inner scope. Oh well, I do C++ on a day-to-day basis, and C# for inhouse tools. Besides, I usually don't have any problems with clashing names, so this is really a non-problem for me. -- Weiter, weiter, ins verderben. Wir müssen leben bis wir sterben. I blog too now[^]
-
Dont be so lazy!
using System;
class B
{
public static void Main()
{
if(true)
{
B b = new B();
}
{ B b = new B(); } // <-- throws no error CS0136
}
}top secret
Download xacc-ide 0.0.3 now!
See some screenshotslol. Reminds me of the line "if you try and make something foolproof, they just make smarter fools" cheers, Chris Maunder
-
using System;
class B
{
public static void Main()
{
if(true)
{
B b = new B();
}B b = new B(); // <-- throws error CS0136 }
}
error CS0136: A local variable named 'b' cannot be declared in this scope because it would give a different meaning to 'b', which is already used in a 'child' scope to denote something else :wtf: :wtf: :wtf:
My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com
Class B has nothing to do with it, I suspect. It means that you can't create a variable inside the scope that would hide a variable that exists outside it, although in this case the order in which they appear means it does not matter. I agree, this could be handled better, and has spat at me more than once. I could be childish and say that they tried to make it simple for migrating VB programmers, but I suspect that, as was the case with switch statements, they identified a place where C++ can cause errors that are hard to find, and they were overzealous. IMO, this should be a warning. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer