41

How to comment multiple lines of code/block of code in VB?

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249

12 Answers12

38

VB doesn't have such a construct at the language level. It has single line comments using apostrophe character:

' hello world
' this is a comment
Rem this is also a comment

However, Visual Studio has a functionality to automate this task. Select the lines you want and press Ctrl+K+C for commenting and Ctrl+K+U for uncommenting (General Development Settings shortcuts, look at the "Edit -> Advanced" menu while selecting some code to see the shortcuts).

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
22

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.

user665301
  • 331
  • 3
  • 4
15

The other answers explain how to comment/uncomment automatically in VB.NET. Just for completeness, in VB6 use these toolbar buttons: alt text. More details here.

Community
  • 1
  • 1
MarkJ
  • 30,070
  • 5
  • 68
  • 111
4

Here is a cludgy way to make a multiline comment which is also collapsible.

    If <![CDATA[ Multiline comment about this and that

Comment about this 
and that and so on
with lots of lines

    ]]> Is Nothing Then : End If

It looks like this when you collapse it

If <![CDATA[ Multiline comment about this and that ...  Is Nothing Then : End If
Magnus
  • 1,584
  • 19
  • 14
  • Does this work for VBScript ? Tried this but got syntax error. (Maybe VB != VBS, n00b here) – yO_ Feb 11 '19 at 13:55
4

Ways you can comment code in VB:

  1. by using CTRL+K+C
  2. adding ' (Apostrophes symbol) in front of your code which you want to make it as comment
  3. adding rem in front of your code which you want to make it as comment. (Note: rem means remarks --> rem is used to include explanatory remarks in the source code of a program.)
  4. just select the particular code and click on the option called "comment out the selected lines" in the toolbar.
  5. #if false (enter your code here) #endif -- the code inside these 2 statements will be commented.
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Laxmi
  • 3,830
  • 26
  • 30
1

in VS2010 VB.NET, type 3 times ' above a class/function/property/declaration

then it will auto generate a comment block :

''' <summary>
''' GRP Business partner ID
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>

same thing in C# but type 3 times /

/// <summary>
/// 
/// </summary>
Andreas
  • 384
  • 1
  • 3
  • 9
1

Select the lines that you want to comment.

Press CTRL+K+C in Visual Studio. It will help you to comment multiple lines at once.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Ananth
  • 41
  • 4
1

Select the lines you need to comment and press keys CTRL+K+C.

If you need to uncomment use CTRL+K+U

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
BGD
  • 219
  • 1
  • 8
1

To comment out a big section of code you highlight the code you want to comment out, then you hit ctrl+K, then hit ctrl+C. To un-comment a block of commented code you hit ctrl+K, then hit ctrl+U.

Hulk1991
  • 3,079
  • 13
  • 31
  • 46
Jared
  • 2,904
  • 6
  • 33
  • 37
0

In the vb++ you can comment the blocks of the comment with:

CommentStart==>Type your comment and on many of the lines<==CommentEnd+/inc.

0

install MZ-Tools and you can use CTRL+SHIFT+C to comment code blocks and CTRL+SHIFT+U to uncomment code blocks.

You can also define your own key-combinations:

MZ-Tools

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Hrqls
  • 2,944
  • 4
  • 34
  • 54
0

Block comments in C-like style /* my comment */ would be useful on e.g. multi-line VB.net statements. They are currently not available. However, in place of writing

myVal = "bla bla bla" /* my comment */ _
      + " more of the same " _
      + " and still more "

You could write:

myVal = "bla bla bla" + 'my comment
        " more of the same " +
        " and still more "

That will work on the later versions of VB.Net.

Wolfgang Grinfeld
  • 870
  • 10
  • 11