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. Damn c# { }'s

Damn c# { }'s

Scheduled Pinned Locked Moved The Lounge
csharpquestioncareer
68 Posts 47 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.
  • Sander RosselS Sander Rossel

    Actually that's a closing brace for your namespace, class, method and if statement. Not much nesting at all, it just looks that way in C# :) This is where I'd usually sing some VB praise, but having been away from VB for far too long I'm just not in the mood :laugh:

    Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

    Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

    Regards, Sander

    R Offline
    R Offline
    RossMW
    wrote on last edited by
    #15

    It's one of the advantages of VB in that the end states what it's the end of. I think that just makes me a bit lazy on the formatting before coding.

    1 Reply Last reply
    0
    • M Mark_Wallace

      Rotate your monitor 90 degrees, to portrait orientation. No kidding.

      I wanna be a eunuchs developer! Pass me a bread knife!

      R Offline
      R Offline
      RossMW
      wrote on last edited by
      #16

      I have been tempted to rotate one of my two monitors, but I have to do other stuff which is best in landscape. Maybe I need more monitors! Keep up that winter tan...

      1 Reply Last reply
      0
      • R RossMW

        Yes, very dimly. Makes it hide to find when you have to scroll to see it. Just have to remember the Left brace, right brace, back arrow, code... dance move...

        S Offline
        S Offline
        Sascha Lefevre
        wrote on last edited by
        #17

        You can change the color of the highlighting :) clicky[^]

        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

        R D 2 Replies Last reply
        0
        • S Sascha Lefevre

          You can change the color of the highlighting :) clicky[^]

          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

          R Offline
          R Offline
          RossMW
          wrote on last edited by
          #18

          Thanks. Changed it to magenta, so now its easy to spot!

          1 Reply Last reply
          0
          • R RossMW

            Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

            S Offline
            S Offline
            Super Lloyd
            wrote on last edited by
            #19

            how about... instead of

            if (condition)
            {
            ....
            }

            use

            if (!condition)
            return;
            ....

            All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

            J 1 Reply Last reply
            0
            • R RossMW

              Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

              J Offline
              J Offline
              justjoshin
              wrote on last edited by
              #20

              The keyboard shortcut "control }" will jump to the matching bracket if you can't immediately identify it. It will work when the cursor is either before or after an opening bracket or a closing bracket. I'd use that command every 5 minutes. very helpful when the scope of a set of brackets extends beyond the visible portion of the page.

              who knows what evil lurks in the hearts of men?

              R 1 Reply Last reply
              0
              • R RossMW

                Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

                V Offline
                V Offline
                virang_21
                wrote on last edited by
                #21

                You need productivity tools for VS ? https://visualstudiogallery.msdn.microsoft.com/dbcb8670-889e-4a54-a226-a48a15e4cace[^]

                Zen and the art of software maintenance : rm -rf * Maths is like love : a simple idea but it can get complicated.

                1 Reply Last reply
                0
                • R RossMW

                  Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

                  G Offline
                  G Offline
                  GuyThiebaut
                  wrote on last edited by
                  #22

                  I code in VB and C# at work - my preference is for C~ due to what I describe as its 'elegance'. Occasionally when I swap over I catch myself adding a semicolon to some VB code however Visual Studio catches it and I soon remember that I am coding on VB. I don't find it too big a deal - being bilingual(English and French), for me it's a similar experience swapping between two programming languages and two spoken languages. Sometimes when I see some horrendous VB code I am heard to exclaim "Put*in de b*rdel de m*rde!" when I meant to comment in English

                  “That which can be asserted without evidence, can be dismissed without evidence.”

                  ― Christopher Hitchens

                  1 Reply Last reply
                  0
                  • R RossMW

                    Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

                    B Offline
                    B Offline
                    Brady Kelly
                    wrote on last edited by
                    #23

                    As others have said, I close braces on opening them (well, I cheat and let ReSharper do that), and I keep the number of braced scopes in a method to a minimum, e.g. avoid nested braces like the plague. The highest level I normally get to is say for one loop or switch at a time.

                    No object is so beautiful that, under certain conditions, it will not look ugly. - Oscar Wilde

                    1 Reply Last reply
                    0
                    • R RossMW

                      Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

                      R Offline
                      R Offline
                      R Erasmus
                      wrote on last edited by
                      #24

                      I stick to languages derived from c. Currently mostly c, java and perl. I have no problems switch between these. Pitch to one of your managers to have the code you're maintaining rewritten in C#... Or rewrite it in C# and tell your boss what you've did and if they'd be interested in changing over to it. To my knowledge there should be no reason to write any code in vb... If we employ people, it is never a requirement to only know vb or only know C#... Its usually in the lines of, do you have vb or C# experience. As the task at hand can usually be performed in either. According to Wikipedia there are 89 different c derivative languages. C obviously had a winning recipe. Your subject should read 'Damn vb (why do they have to think they are special and different)'.

                      "Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>

                      1 Reply Last reply
                      0
                      • R RossMW

                        Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

                        D Offline
                        D Offline
                        deostroll
                        wrote on last edited by
                        #25

                        Maintain codebase in vb. There is nothing different between the two...except may be for productivity features enabled through use of the ide. There are probably other ide that give better productivity support. Try sharp develop for a change : [^]

                        1 Reply Last reply
                        0
                        • R RossMW

                          Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

                          V Offline
                          V Offline
                          V 0
                          wrote on last edited by
                          #26

                          * I usually mark them with a comment (end if, end for, ...), some find it childish, but I found it often helpful * setup VS to clearly show matching braces * ALWAYS use the {} even if there is only one statement. Most common error for me if someone else wrote something like (cause I never, ever do that):

                          if [statement a]
                          else if [statement b]
                          else if [statement c]
                          else if [statement d]
                          else if [statement e]
                          else if [statement f]
                          else [statement g]

                          and you need to insert an additional statement in between somewhere. hope this helps.

                          V.
                          (MQOTD rules and previous solutions)

                          R 1 Reply Last reply
                          0
                          • J justjoshin

                            The keyboard shortcut "control }" will jump to the matching bracket if you can't immediately identify it. It will work when the cursor is either before or after an opening bracket or a closing bracket. I'd use that command every 5 minutes. very helpful when the scope of a set of brackets extends beyond the visible portion of the page.

                            who knows what evil lurks in the hearts of men?

                            R Offline
                            R Offline
                            RossMW
                            wrote on last edited by
                            #27

                            Never knew that one. I'll have to give it a go. :)

                            1 Reply Last reply
                            0
                            • R RossMW

                              Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

                              Z Offline
                              Z Offline
                              Zafar Sultan
                              wrote on last edited by
                              #28

                              I have a habit of typing } followed by back key as soon as I type {. This keeps things under control as the chances to forget typing } are none. I would also suggest to try outlining for visual studio. Try this[^]. Using this you will be able to work in the current block while collapsing the other code blocks. Another thing I would like to suggest is the use of ctrl+K+D(for code formatting), Ctrl+M+O and Ctrl+M+P to toggle outlining. I find these combinations very handy while coding.

                              R 1 Reply Last reply
                              0
                              • V V 0

                                * I usually mark them with a comment (end if, end for, ...), some find it childish, but I found it often helpful * setup VS to clearly show matching braces * ALWAYS use the {} even if there is only one statement. Most common error for me if someone else wrote something like (cause I never, ever do that):

                                if [statement a]
                                else if [statement b]
                                else if [statement c]
                                else if [statement d]
                                else if [statement e]
                                else if [statement f]
                                else [statement g]

                                and you need to insert an additional statement in between somewhere. hope this helps.

                                V.
                                (MQOTD rules and previous solutions)

                                R Offline
                                R Offline
                                RossMW
                                wrote on last edited by
                                #29

                                Never thought of the obvious of commenting the end brace. Doh I have setup the ide to have a brighter highlight but I notice it sometimes does work. Generally if there is some other minor error..:confused:

                                1 Reply Last reply
                                0
                                • Z Zafar Sultan

                                  I have a habit of typing } followed by back key as soon as I type {. This keeps things under control as the chances to forget typing } are none. I would also suggest to try outlining for visual studio. Try this[^]. Using this you will be able to work in the current block while collapsing the other code blocks. Another thing I would like to suggest is the use of ctrl+K+D(for code formatting), Ctrl+M+O and Ctrl+M+P to toggle outlining. I find these combinations very handy while coding.

                                  R Offline
                                  R Offline
                                  RossMW
                                  wrote on last edited by
                                  #30

                                  The tool looks good. Going to give it a go when I can. Thanks

                                  Z 1 Reply Last reply
                                  0
                                  • R RossMW

                                    The tool looks good. Going to give it a go when I can. Thanks

                                    Z Offline
                                    Z Offline
                                    Zafar Sultan
                                    wrote on last edited by
                                    #31

                                    You're welcome :)

                                    1 Reply Last reply
                                    0
                                    • S Super Lloyd

                                      how about... instead of

                                      if (condition)
                                      {
                                      ....
                                      }

                                      use

                                      if (!condition)
                                      return;
                                      ....

                                      All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                                      J Offline
                                      J Offline
                                      JeremyBob
                                      wrote on last edited by
                                      #32

                                      Not just that, but be consistent in your if statements if you don't want braces issue. i.e. ALWAYS use the braces, even in the above !condition case. Personally I find Resharper is an excellent tool to help "flatten" you code by suggesting inverting your if statements.

                                      if (!condition)
                                      {
                                      return;
                                      }

                                      S H 2 Replies Last reply
                                      0
                                      • J JeremyBob

                                        Not just that, but be consistent in your if statements if you don't want braces issue. i.e. ALWAYS use the braces, even in the above !condition case. Personally I find Resharper is an excellent tool to help "flatten" you code by suggesting inverting your if statements.

                                        if (!condition)
                                        {
                                        return;
                                        }

                                        S Offline
                                        S Offline
                                        Super Lloyd
                                        wrote on last edited by
                                        #33

                                        I am consistent!!!

                                        If (one short liner or set value) put on the same line
                                        else if (one long line) put below
                                        else use brace!

                                        See, I even use if to define if behavior! Top that! ;P

                                        All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                                        1 Reply Last reply
                                        0
                                        • R RossMW

                                          Now, I am not interested in a VB versus C# debate but. In my job I only spend about 20% of my time coding. Being from a C# background anything new I would normally do in C# (and yes very occasionally VB), but I also have to maintain old VB code (and heaven forbid, very occasionally VB6). Normally I spent a month or two in each language (depending on the task at hand) and are happy in any camp. Changing back and forth between languages is relatively straight forward, but lately I noticed it takes me longer to get back into swing of C#. And the reason.. Well, I think its because I've got myself into bad c# typing habits from using vb. I seem to be wasting so time chasing missing / misplaced { }, forgetting semicolons case sensitivity and ()'s. Now semicolons, case and () problems are just a "Oh Bugger" moment, but as for missing / misplaced { } 's. They can be time waster. For all you pro c# developers, Do you have any tips on keeping these damn { } under control?

                                          S Offline
                                          S Offline
                                          Slacker007
                                          wrote on last edited by
                                          #34

                                          RossMW wrote:

                                          They can be time waster.

                                          They have never been a time waster for me. I have never given it a second thought. Because you don't do this much, you will notice "everything", I would guess.

                                          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