Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Hey CG, C# is pretty weird too

Hey CG, C# is pretty weird too

Scheduled Pinned Locked Moved The Lounge
csharpc++comarchitecturehelp
28 Posts 15 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Nish Nishant

    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

    M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #6

    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

    N F G J 4 Replies Last reply
    0
    • M Marc Clifton

      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

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #7

      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

      M 1 Reply Last reply
      0
      • N Nish Nishant

        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

        N Offline
        N Offline
        Nemanja Trifunovic
        wrote on last edited by
        #8

        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.

        N 1 Reply Last reply
        0
        • M Marc Clifton

          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

          F Offline
          F Offline
          Franz Pentenrieder
          wrote on last edited by
          #9

          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?

          M 1 Reply Last reply
          0
          • N Nemanja Trifunovic

            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.

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #10

            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-block

            	B 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

            N P R 3 Replies Last reply
            0
            • F Franz Pentenrieder

              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?

              M Offline
              M Offline
              Marc Clifton
              wrote on last edited by
              #11

              Franz Pentenrieder wrote: Are there any open source VB.NET projects? Oops. I meant to say compilers. Marc MyXaml Advanced Unit Testing

              1 Reply Last reply
              0
              • N Nish Nishant

                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-block

                	B 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

                N Offline
                N Offline
                Nemanja Trifunovic
                wrote on last edited by
                #12

                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.

                1 Reply Last reply
                0
                • N Nish Nishant

                  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

                  M Offline
                  M Offline
                  Marc Clifton
                  wrote on last edited by
                  #13

                  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

                  1 Reply Last reply
                  0
                  • N Nish Nishant

                    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-block

                    	B 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

                    P Offline
                    P Offline
                    ProffK
                    wrote on last edited by
                    #14

                    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.

                    1 Reply Last reply
                    0
                    • N Nish Nishant

                      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

                      L Offline
                      L Offline
                      leppie
                      wrote on last edited by
                      #15

                      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

                      S C 2 Replies Last reply
                      0
                      • M Marc Clifton

                        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

                        G Offline
                        G Offline
                        Giancarlo Aguilera
                        wrote on last edited by
                        #16

                        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

                        1 Reply Last reply
                        0
                        • N Nish Nishant

                          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-block

                          	B 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

                          R Offline
                          R Offline
                          Russell Morris
                          wrote on last edited by
                          #17

                          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

                          1 Reply Last reply
                          0
                          • N Nish Nishant

                            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

                            R Offline
                            R Offline
                            Richard Stringer
                            wrote on last edited by
                            #18

                            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)

                            1 Reply Last reply
                            0
                            • N Nish Nishant

                              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

                              T Offline
                              T Offline
                              Tim Ranker
                              wrote on last edited by
                              #19

                              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

                              J 1 Reply Last reply
                              0
                              • M Marc Clifton

                                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

                                J Offline
                                J Offline
                                Judah Gabriel Himango
                                wrote on last edited by
                                #20

                                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

                                1 Reply Last reply
                                0
                                • L leppie

                                  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

                                  S Offline
                                  S Offline
                                  S Senthil Kumar
                                  wrote on last edited by
                                  #21

                                  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.

                                  J 1 Reply Last reply
                                  0
                                  • N Nish Nishant

                                    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

                                    J Offline
                                    J Offline
                                    Jorgen Sigvardsson
                                    wrote on last edited by
                                    #22

                                    I noticed that one too. Fascist compiler. :mad: -- Weiter, weiter, ins verderben. Wir müssen leben bis wir sterben. I blog too now[^]

                                    1 Reply Last reply
                                    0
                                    • T Tim Ranker

                                      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

                                      J Offline
                                      J Offline
                                      Jorgen Sigvardsson
                                      wrote on last edited by
                                      #23

                                      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[^]

                                      1 Reply Last reply
                                      0
                                      • S S Senthil Kumar

                                        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.

                                        J Offline
                                        J Offline
                                        Jorgen Sigvardsson
                                        wrote on last edited by
                                        #24

                                        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[^]

                                        S 1 Reply Last reply
                                        0
                                        • J Jorgen Sigvardsson

                                          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[^]

                                          S Offline
                                          S Offline
                                          S Senthil Kumar
                                          wrote on last edited by
                                          #25

                                          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

                                          J 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups