views:

179

answers:

1

I commented on this answer some time ago regarding how visual studio comments out code with // or /* */. I was thinking to revise the answer (to include my findings) but I had to test it first, which kind of confused me.

My finding is that depending on what you comment when you press Ctrl - K, Ctrl - C you will get either // or /* */.

First example:

<start selection here>    code();
                          someCall();
                          thirdCall();<end selection here>

this will produce the following:

//code();
//someCall();
//thirdCall();

Second example:

    <start selection here>code();
                          someCall();
                          thirdCall();<end selection here>

this will produce the following:

/*code();
someCall();
thirdCall();*/

Third example

    <start selection here>code();
                          //someCall();
                          thirdCall();<end selection here>

this will produce the following:

//code();
////someCall();
//thirdCall();

Note that example 2 and 3 is the exact same selection, but the comment makes Visual Studio interpret it differently.

Why is this?

+1  A: 

The approach one would expect is to use // for any selection that is made up entirely of complete lines, and /*...*/ for anything that starts/ends mid-way along a line.

...which is what it seems to actually do.

Jason Williams
What do you mean by complete lines?
Default
If you put the cursor at the left edge (on column 0) and then press shift+down, you will select a complete line. That is, all the text from column 0 to the end of the line, plus the newline at the end.
Jason Williams
exactly, that is [1]. But when selecting from column 4 (where the text starts) you get /* */. I hope my edit made it clearer.. I also found out that it has something to do with comments..?
Default
@Jason: I rewrote the question, hopefully it's much clearer now
Default
It seems this is a C++ specific behaviour. I can understand it using // instead of /* */ if the lines to comment include a multiline comment (because they cannot be nested). I presume that if you include a // comment it is taken as a "hint" that you simply prefer // comments to /* ones?
Jason Williams