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
-
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
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
-
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
-
Interesting. One would expect the child scope declaration to be illegal, because the variable b already exists in the outer scope. His hands felt the grasp of strong white hairs, and he knew he would not survive this fungus.
ProffK wrote: One would expect the child scope declaration to be illegal, because the variable b already exists in the outer scope. Actually, one wouldn't expect anything of the sort if one previously came from a C++ backgroud This C++ code compiles fine :-
class B
{
};void main()
{
if(1)
{
B* b = new B();
}B\* b = new B();
}
My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com
-
ProffK wrote: One would expect the child scope declaration to be illegal, because the variable b already exists in the outer scope. Actually, one wouldn't expect anything of the sort if one previously came from a C++ backgroud This C++ code compiles fine :-
class B
{
};void main()
{
if(1)
{
B* b = new B();
}B\* b = new B();
}
My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com
Nishant S wrote: This C++ code compiles fine :- And so does this C++/CLI code lest someone thinks it's a .NET issue :-
ref class B
{
};void main()
{
if(1)
{
B^ b = gcnew B();
}B^ b = gcnew B();
}
My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com
-
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
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
-
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: Uh, the "throws error" really got me confused there for a minute, thinking that the runtime threw an exception. Sorry about that :-) Marc Clifton wrote: As compiler errors go, this one is both annoying, coming from C++, and has saved my butt several times. How does it save you? If it's okay, could you give a possible scenario (not a contrived one, but one you actually faced)? Marc Clifton wrote: I haven't seen any C# to VB.NET converters. Why is that? :eek: Don't give someone ideas, Marc!
My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com
-
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
Yep, that's in accordance with the C# Language Specifcation - 3.3 Declarations[^] :|
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
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 projects? i don't like vb.net too, but to be fair - i think DotNetNuke[^] is written in it?
-
Yep, that's in accordance with the C# Language Specifcation - 3.3 Declarations[^] :|
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
Nemanja Trifunovic wrote: Yep, that's in accordance with the C# Language Specifcation - 3.3 Declarations[^] I think the wording is quite confusing. It says :- The scope of a local variable declared in a local-variable-declaration is the block in which the declaration occurs. And it defines block as :- A block consists of an optional statement-list (Section 8.2.1), enclosed in braces. If the statement list is omitted, the block is said to be empty. A block may contain declaration statements (Section 8.5). The scope of a local variable or constant declared in a block is the block. So in this code :-
using System;
class B
{
//start function-block
public static void Main()
{
//start for-block
for(int i=0; i<10; i++)
{
B b = new B(); // this is in the for-block
}
//stop for-blockB b = new B(); //this is in the function-block } //stop function-block
}
I don't see any reason why the 2nd declaration of b should give a compiler error :-(
My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com
-
Marc Clifton wrote: Are there any open source VB.NET projects? i don't like vb.net too, but to be fair - i think DotNetNuke[^] is written in it?
Franz Pentenrieder wrote: Are there any open source VB.NET projects? Oops. I meant to say compilers. Marc MyXaml Advanced Unit Testing
-
Nemanja Trifunovic wrote: Yep, that's in accordance with the C# Language Specifcation - 3.3 Declarations[^] I think the wording is quite confusing. It says :- The scope of a local variable declared in a local-variable-declaration is the block in which the declaration occurs. And it defines block as :- A block consists of an optional statement-list (Section 8.2.1), enclosed in braces. If the statement list is omitted, the block is said to be empty. A block may contain declaration statements (Section 8.5). The scope of a local variable or constant declared in a block is the block. So in this code :-
using System;
class B
{
//start function-block
public static void Main()
{
//start for-block
for(int i=0; i<10; i++)
{
B b = new B(); // this is in the for-block
}
//stop for-blockB b = new B(); //this is in the function-block } //stop function-block
}
I don't see any reason why the 2nd declaration of b should give a compiler error :-(
My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com
Arrrgh. Sorry, Nish. The url I left is wrong. It does not point to Chapter 3.3[^] at all.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
Marc Clifton wrote: Uh, the "throws error" really got me confused there for a minute, thinking that the runtime threw an exception. Sorry about that :-) Marc Clifton wrote: As compiler errors go, this one is both annoying, coming from C++, and has saved my butt several times. How does it save you? If it's okay, could you give a possible scenario (not a contrived one, but one you actually faced)? Marc Clifton wrote: I haven't seen any C# to VB.NET converters. Why is that? :eek: Don't give someone ideas, Marc!
My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com
Nishant S wrote: How does it save you? I don't have any specific examples lying around because I fix them. :-D However, the basic idea is this--I may have written some code that instantiates a class. Later on, I need to add some rules that instantiate different specialized classes. Original code:
B b=new B();
Now, let's say I'm being braindead:
if (foobar)
{
B b; // [edited] I incorrectly declare B here...
if (fizbin)
{
b=new B1(); // because we're going to assign it here
}
else
{
b=new B2();
}
}
... more code ...
B b=new B(); // but I forget about this instantiation!Now, I've forgotten that I don't want to initialize a new B later on. Yes, I've written code similar to this, and the compiler is correct in flagging this as an error. Yeah, it's a bit contrived, but it illustrates when the compiler has saved me from a debugging session and my own stupidity. Marc MyXaml Advanced Unit Testing
-
Nemanja Trifunovic wrote: Yep, that's in accordance with the C# Language Specifcation - 3.3 Declarations[^] I think the wording is quite confusing. It says :- The scope of a local variable declared in a local-variable-declaration is the block in which the declaration occurs. And it defines block as :- A block consists of an optional statement-list (Section 8.2.1), enclosed in braces. If the statement list is omitted, the block is said to be empty. A block may contain declaration statements (Section 8.5). The scope of a local variable or constant declared in a block is the block. So in this code :-
using System;
class B
{
//start function-block
public static void Main()
{
//start for-block
for(int i=0; i<10; i++)
{
B b = new B(); // this is in the for-block
}
//stop for-blockB b = new B(); //this is in the function-block } //stop function-block
}
I don't see any reason why the 2nd declaration of b should give a compiler error :-(
My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com
The wording is quite clear on the scope of a local variable declared in a block, but says nothing about local variables decalred outside the block. "Within the scope of a local variable, it is a compile-time error to declare another local variable or constant with the same name." The scope of the latter 'b' includes the block scope, so you are declaring another variable 'within ' its scope. His hands felt the grasp of strong white hairs, and he knew he would not survive this fungus.
-
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
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 screenshots -
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
sorry to butt in, I know am not liked much around here. "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." Yes, VB is whatever MS wants it to be. VB exists for the sole purpose of making money, something it has been very successful at. I doubt MS would even consider making VB a standard. What for? So that others can make money of it too? MS, sharing? Furthermore, even if MS wanted to do so, it would be almost impossible, although given MS's power and money nothing seems impossible, since there's no way the academics would accept the language. VB will always be MS windows specific. "Is VB.NET getting generics?" Yes, the only feature up until now that VB is not getting is anonymous methods. "Are there any open source VB.NET projectscompilers?" None that I know of. Wouldn't you have to gave MS's consent in order to do so? "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)." Many VB COM projects were 1) converted to VB.NET via the upgrade wizard, 2) what was not successfully converted was fixed manually, and then 3) finally the resulting vb.net project was converted to C#. Why would anyone want to convert a C# project to vb.net? Its an after the fact issue, if you know what I mean. My experience has shown me that many COM projects were taken in VB and not C++ because of RAD, exclusively, but had VC++ offered the same RADness, the project would have been done in C++. So now many of these COM projects that would have been written in C++ but instead were written in VB COM because of RAD, are now being converted to C#. This is just my opinion. Giancarlo
-
Nemanja Trifunovic wrote: Yep, that's in accordance with the C# Language Specifcation - 3.3 Declarations[^] I think the wording is quite confusing. It says :- The scope of a local variable declared in a local-variable-declaration is the block in which the declaration occurs. And it defines block as :- A block consists of an optional statement-list (Section 8.2.1), enclosed in braces. If the statement list is omitted, the block is said to be empty. A block may contain declaration statements (Section 8.5). The scope of a local variable or constant declared in a block is the block. So in this code :-
using System;
class B
{
//start function-block
public static void Main()
{
//start for-block
for(int i=0; i<10; i++)
{
B b = new B(); // this is in the for-block
}
//stop for-blockB b = new B(); //this is in the function-block } //stop function-block
}
I don't see any reason why the 2nd declaration of b should give a compiler error :-(
My blog on C++/CLI, MFC/Win32, .NET - void Nish(char* szBlog); My MVP tips, tricks and essays web site - www.voidnish.com
What the C# language specification is saying is that the scope of the variable 'b' declared in the primary block for Main(), just after the for() loop, is in fact the entire Main() block, not just the part of that block after the for loop. The code you've posted is equivalent to the following code:
class B
{
//start function block
public static void Main()
{
B b = null;//start for-block
for(int i=0;i<10;i++)
{
B b = new B();
}b = new B();
}
}In essence, any variable declarations you make in a block, regardless of where you make them in the block, are treated as if those declarations were made at the very beginning of the block itself. This is, I believe, in contrast with standard C and C++ convention, where a local variable's scope is the portion of the block in which it is declared after the declaration. I seem to remember that prior standards for C (maybe C++ as well, I'm not sure...) insisted that all local variables be declared at the beginning of the block to which they belonged. I don't believe that is the case any longer, but I could be wrong. -- Russell Morris "So, broccoli, mother says you're good for me... but I'm afraid I'm no good for you!" - Stewy
-
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)
-
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
-
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