Lack of block comments in VB .NET?
Asked Answered
B

5

13

Just a question of interest: Does anyone know why there's no block comment capability in VB .NET? (Unless there really is - but I've never yet come across it.)

Boson answered 4/2, 2010 at 0:36 Comment(4)
To further deter people from using Visual Basic? :-pCharie
Take a look: connect.microsoft.com/VisualStudio/feedback/details/436415/…Delorsedelos
I select multiple lines and hit the comment/uncomment button in the IDE, so it doesn't matter to me :)Homicidal
See also the question about how to comment multiple lines in one operationExtradition
A
18

It is a side-effect of the Visual Basic syntax, a new-line terminates a statement. That makes a multi-line comment pretty incompatible with the basic way the compiler parses the language. Not an issue in the curly brace languages, new-lines are just white space.

It has never been a real problem, Visual Basic has had strong IDE support for a very long time. Commenting out multiple lines is an IDE feature, Edit + Advanced + Comment Selection.

Alansen answered 4/2, 2010 at 0:54 Comment(3)
+1 The IDE handles all of this stuff for me, allowing me to focus on writing good code that should never need to be commented outHomicidal
With CTRL+K+C and CTRL+K+U you don't need block comments :)Erk
Also - in C#, when you write /*, the editor automaticaly converts all code, that follows, to comment, so you loose outlining. That's why I don't use block comments in C# also.Erk
K
10

Totally abusing compiler directives here... but:

#If False Then
Comments
go
here
#End If

You don't get the benefits of proper code coloration (it doesn't show in green when using the default color scheme) and the implicit line-continuation system automatically indents lines in a paragraph starting at the second line. But the compiler will ignore the text.

Kristalkristan answered 11/5, 2011 at 0:29 Comment(1)
Perfect! Needed to temporarily comment out some lines of code in a SSRS deploy script in Notepad++ and this worked great.Ietta
W
2

As can be read in “Comments in Code“ there isn't any other way:

If your comment requires more than one line, use the comment symbol on each line, as the following example illustrates.

' This comment is too long to fit on a single line, so we break 
' it into two lines. Some comments might need three or more lines.

Similarly, the help on the REM statement states:

Note:
You cannot continue a REM statement by using a line-continuation sequence (_). Once a comment begins, the compiler does not examine the characters for special meaning. For a multiple-line comment, use another REM statement or a comment symbol (') on each line.

Wadesworth answered 4/2, 2010 at 0:41 Comment(0)
D
1

Depending on how many lines are to be ignored, one can use compiler directives instead. It may not be technically equivalent to comments (you don't get the syntax coloring of comments, for example), but it gets the job done without commenting many lines individually. So you just add 3 more lines of code.

#Const COMMENT = "C"
'basically a false statement
#If COMMENT = "Y"  Then
   'code to be commented goes between #If and #End If
   MsgBox('Commenting failed!')
#End If

This is assuming the purpose is for ignoring blocks of code instead of adding documentation (what "comments" are actually used for, but I also wouldn't mind using compiler directives for that).

The effort required however, makes this method inconvenient when there are just around 10 lines to comment.

Reference: http://msdn.microsoft.com/en-us/library/tx6yas69.aspx

Daciadacie answered 1/3, 2011 at 16:1 Comment(0)
V
0

Since VB.Net supports XML, you could write the following in your code:

Dim ignored =
    <!-- 
             here my multiline 
             block comment 
    -->

It will even make the lines in comment-green color.

Vaud answered 23/1 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.