Share via


Making Notes in Your Programs: Using Comments

In this lesson, you will learn how to create comments in the code for your programs.

The code that makes up a program can be difficult to read and understand, particularly if you are not the person who originally wrote it. By using comments, you can create notes to yourself or to other users of your code.

Comments are text entries in the Code Editor that are ignored by the Visual Basic compiler when the program is run. Thus, you can write a note to yourself that explains what a particular section of your program does, or that reminds you to finish uncompleted programming tasks.

You create a comment by starting a line with the ' character. The following example demonstrates how to create a comment.

' This is a comment. WOW!

You can add comments to the ends of lines, as well, also by using the ' character. This is frequently done to provide comments about individual lines of code, as seen in the following example.

MsgBox("Hello World!") ' This line causes a message box to appear.

As with single-line comments, anything following the ' character on that line is ignored by the program.

Using Comments for Debugging

Another common use for comments is to temporarily prevent a line of code from executing while you debug your program. For example, suppose you had a line of code that displayed a message box.

MsgBox("Hello World!")

If you wanted to run the program without displaying it, but did not want to delete it permanently, you could use the comment character (') to temporarily hide it from your program, as shown below.

' MsgBox("Hello World!")

Because everything after the ' character is ignored, the program will run without executing this line. You can later remove the ' character, and the message box will be displayed.

Try It!

To insert comments

  1. On the File menu, choose NewProject.

  2. On the Templates pane in the New Project dialog box, click Windows Application.

  3. In the Name box, type Comments and then click OK.

    A new Windows Forms project opens.

  4. Double-click the form to open the Code Editor.

  5. In the Form1_Load event handler, type the following code.

    ' This code will cause two message boxes to appear
    MsgBox("This is Message Box 1") ' Display Message Box 1
    MsgBox("This is Message Box 2") ' Display Message Box 2
    
  6. Press F5 to run the program.

    The program starts, and each of the two message boxes are shown in turn.

  7. On the Debug menu, choose Stop Debugging to end the program.

  8. In the Code Editor, add a comment character (') to the first message-box line, so it reads as follows.

    ' MsgBox("This is MessageBox 1") ' Ignore Message Box 1
    
  9. Press F5 to run the program.

    Notice that this time, the first message-box line is ignored by the program, and only the second message box is shown.

Next Steps

In this lesson, you learned to use comments to create notes in your code and to temporarily inactivate individual lines of code. This completes the lessons on debugging. In the next set of lessons, you will learn how to use a database to store and retrieve information for your program.

Next Lesson: Managing Your Records: Using Data in Your Program

See Also

Concepts

Comments in Code

Other Resources

What Went Wrong? Finding and Fixing Errors Through Debugging