C# "oddity"
-
*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.
-
*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.
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
-
*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.
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
-
*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.
Well... given the compiler should ignore the comment anyway, and if whitespace is ignored, maybe that's why it's ok?
-
*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.
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 -
*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.
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.
-
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
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
-
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
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
-
*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.
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
; -
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
;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.
-
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
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! ) -”-”-
-
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! ) -”-”-
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.
-
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.
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