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. C# "oddity"

C# "oddity"

Scheduled Pinned Locked Moved The Lounge
questioncsharpvisual-studiohelp
13 Posts 11 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.
  • A Anton Afanasyev

    *not a programming question* I was just sitting in VS2005, writing in C#. Then I wrote something like

    lblName.Text =

    // some comment here
    lblOccupation.Text = row["Occupation"].ToString();

    and saved. And then left. In the meantime, VS managed to crash itself. When I started it back up again, first thing I did was compile the project (sometimes I do this just to find places where I left off last time :p And..it compiled. No warning, no error, nothing. It compiled. I don't know what the C# spec says, but logically, this kind of thing should not compile. Or should it? I mean, the C-family languages allow assignment statements (among others) to be split across lines, but this has a comment in between! Anyway, that's my oddity for the day.

    C Offline
    C Offline
    Colin Angus Mackay
    wrote on last edited by
    #3

    Anton Afanasyev wrote:

    I don't know what the C# spec says, but logically, this kind of thing should not compile.

    Why not? All the comment is until the end of line. A statement is until the semi-colon and can be spread across as many lines as you want.

    Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * Follow up on hiring a software developer * The Value of Smaller Methods My website | blog

    1 Reply Last reply
    0
    • A Anton Afanasyev

      *not a programming question* I was just sitting in VS2005, writing in C#. Then I wrote something like

      lblName.Text =

      // some comment here
      lblOccupation.Text = row["Occupation"].ToString();

      and saved. And then left. In the meantime, VS managed to crash itself. When I started it back up again, first thing I did was compile the project (sometimes I do this just to find places where I left off last time :p And..it compiled. No warning, no error, nothing. It compiled. I don't know what the C# spec says, but logically, this kind of thing should not compile. Or should it? I mean, the C-family languages allow assignment statements (among others) to be split across lines, but this has a comment in between! Anyway, that's my oddity for the day.

      M Offline
      M Offline
      martin_hughes
      wrote on last edited by
      #4

      Well... given the compiler should ignore the comment anyway, and if whitespace is ignored, maybe that's why it's ok?

      1 Reply Last reply
      0
      • A Anton Afanasyev

        *not a programming question* I was just sitting in VS2005, writing in C#. Then I wrote something like

        lblName.Text =

        // some comment here
        lblOccupation.Text = row["Occupation"].ToString();

        and saved. And then left. In the meantime, VS managed to crash itself. When I started it back up again, first thing I did was compile the project (sometimes I do this just to find places where I left off last time :p And..it compiled. No warning, no error, nothing. It compiled. I don't know what the C# spec says, but logically, this kind of thing should not compile. Or should it? I mean, the C-family languages allow assignment statements (among others) to be split across lines, but this has a comment in between! Anyway, that's my oddity for the day.

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #5

        The result of an assignment is the value assigned. All lines beginning with // are ignored. Your code could similarly be written as: lblName.Text = /*Do an assignment*/ lblOccupation.Text = row["Occupation"].ToString();

        Need a C# Consultant? I'm available.
        Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

        1 Reply Last reply
        0
        • A Anton Afanasyev

          *not a programming question* I was just sitting in VS2005, writing in C#. Then I wrote something like

          lblName.Text =

          // some comment here
          lblOccupation.Text = row["Occupation"].ToString();

          and saved. And then left. In the meantime, VS managed to crash itself. When I started it back up again, first thing I did was compile the project (sometimes I do this just to find places where I left off last time :p And..it compiled. No warning, no error, nothing. It compiled. I don't know what the C# spec says, but logically, this kind of thing should not compile. Or should it? I mean, the C-family languages allow assignment statements (among others) to be split across lines, but this has a comment in between! Anyway, that's my oddity for the day.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #6

          Similar things work in C (with the // extension), C++, and Java too.

          Luc Pattyn [Forum Guidelines] [My Articles]


          This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


          1 Reply Last reply
          0
          • C Christopher Duncan

            Since comments are invisible, what you have is a = b = c; And of course, that's perfectly legit.

            Christopher Duncan Author of The Career Programmer and Unite the Tribes Coming soon: Got a career question? Ask the Attack Chihuahua! www.PracticalUSA.com

            R Offline
            R Offline
            Rama Krishna Vavilala
            wrote on last edited by
            #7

            Wohoo! You got your reply link back!:) When did it happen?:)

            You have, what I would term, a very formal turn of phrase not seen in these isles since the old King passed from this world to the next. martin_hughes on VDK

            C 1 Reply Last reply
            0
            • R Rama Krishna Vavilala

              Wohoo! You got your reply link back!:) When did it happen?:)

              You have, what I would term, a very formal turn of phrase not seen in these isles since the old King passed from this world to the next. martin_hughes on VDK

              C Offline
              C Offline
              Christopher Duncan
              wrote on last edited by
              #8

              After Chris threatened the hamsters with electro-shock therapy. :)

              Christopher Duncan Author of The Career Programmer and Unite the Tribes Coming soon: Got a career question? Ask the Attack Chihuahua! www.PracticalUSA.com

              1 Reply Last reply
              0
              • A Anton Afanasyev

                *not a programming question* I was just sitting in VS2005, writing in C#. Then I wrote something like

                lblName.Text =

                // some comment here
                lblOccupation.Text = row["Occupation"].ToString();

                and saved. And then left. In the meantime, VS managed to crash itself. When I started it back up again, first thing I did was compile the project (sometimes I do this just to find places where I left off last time :p And..it compiled. No warning, no error, nothing. It compiled. I don't know what the C# spec says, but logically, this kind of thing should not compile. Or should it? I mean, the C-family languages allow assignment statements (among others) to be split across lines, but this has a comment in between! Anyway, that's my oddity for the day.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #9

                Yeah, no problem there.

                lblName
                // comment
                .
                // comment
                Text
                // comment

                // comment
                lblOccupation
                // comment
                .
                // comment
                Text
                // comment

                // comment
                row
                // comment
                [
                // comment
                "Occupation"
                ]
                // comment
                .
                // comment
                ToString
                // comment
                (
                // comment
                )
                // comment
                ;

                L 1 Reply Last reply
                0
                • P PIEBALDconsult

                  Yeah, no problem there.

                  lblName
                  // comment
                  .
                  // comment
                  Text
                  // comment

                  // comment
                  lblOccupation
                  // comment
                  .
                  // comment
                  Text
                  // comment

                  // comment
                  row
                  // comment
                  [
                  // comment
                  "Occupation"
                  ]
                  // comment
                  .
                  // comment
                  ToString
                  // comment
                  (
                  // comment
                  )
                  // comment
                  ;

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #10

                  it is a good thing to add comments as long as they are informative rather than reflective. :-D

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                  1 Reply Last reply
                  0
                  • C Christopher Duncan

                    Since comments are invisible, what you have is a = b = c; And of course, that's perfectly legit.

                    Christopher Duncan Author of The Career Programmer and Unite the Tribes Coming soon: Got a career question? Ask the Attack Chihuahua! www.PracticalUSA.com

                    C Offline
                    C Offline
                    chaiguy1337
                    wrote on last edited by
                    #11

                    I agree that it is (and should be) perfectly legal, however clearly in this case a warning might have been handy. I like how C# will warn you if you say something like this: if ( myBool = true ) { ... } ...even though that, technically is also legal. :)

                    "So what's the future like? Are there flying cars and everything's clean?" "No, the cars are still on the ground and it's even dirtier, but we're working on it." From: Quantum Leap (not verbatim) {o,o}.oO( Want a great RSS reader? Try FeedBeast! ) |)””’)      ( Check out my profile for a special CodeProject deal! ) -”-”-

                    P 1 Reply Last reply
                    0
                    • C chaiguy1337

                      I agree that it is (and should be) perfectly legal, however clearly in this case a warning might have been handy. I like how C# will warn you if you say something like this: if ( myBool = true ) { ... } ...even though that, technically is also legal. :)

                      "So what's the future like? Are there flying cars and everything's clean?" "No, the cars are still on the ground and it's even dirtier, but we're working on it." From: Quantum Leap (not verbatim) {o,o}.oO( Want a great RSS reader? Try FeedBeast! ) |)””’)      ( Check out my profile for a special CodeProject deal! ) -”-”-

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #12

                      In this case, C# is protecting you from yourself because this ISN'T legal. That's C thinking, and would mean that allowing if (myval = 1) would also equate to true. The result of an assignment operation is not allowed in an if condition as a boolean test.

                      logan1337 wrote:

                      I agree that it is (and should be) perfectly legal, however clearly in this case a warning might have been handy

                      Why? The ability to split a single line of code has long been accepted and makes code more meaningful. I hated the way that VB made you use the underscore to indicate that lines were being continued.

                      Deja View - the feeling that you've seen this post before.

                      My blog | My articles

                      D 1 Reply Last reply
                      0
                      • P Pete OHanlon

                        In this case, C# is protecting you from yourself because this ISN'T legal. That's C thinking, and would mean that allowing if (myval = 1) would also equate to true. The result of an assignment operation is not allowed in an if condition as a boolean test.

                        logan1337 wrote:

                        I agree that it is (and should be) perfectly legal, however clearly in this case a warning might have been handy

                        Why? The ability to split a single line of code has long been accepted and makes code more meaningful. I hated the way that VB made you use the underscore to indicate that lines were being continued.

                        Deja View - the feeling that you've seen this post before.

                        My blog | My articles

                        D Offline
                        D Offline
                        DavidNohejl
                        wrote on last edited by
                        #13

                        Pete O'Hanlon wrote:

                        In this case, C# is protecting you from yourself because this ISN'T legal.

                        It is legal because bool b = true; if (b = false) { } compiles (with warning). Maybe in case of a = b = c; there could be warning, possibly missing one "=", as in a = b == c; In that case warning could make sense, but then again, a = b = c is probably just as often what you really wanted as a = (b == c).


                        [My Blog]
                        "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
                        "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

                        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