Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
As with all good programming languages, both Visual Basic and C# support the ability to add comments to your code. You can use comments to document features in your code that otherwise might not be easy to figure out, or to temporarily block certain code from executing. I will demonstrate how to add comments in both languages. I'll start with the file LoopingCSharp.aspx. This page already has the functionality to execute a couple of loops, a For loop and a While loop. I'll save the file under a new name selecting File > Save As. I'll name the file CommentsCSharp.aspx.
You can comment code in C# in one of two ways. You can either use beginning of line comments which are double slash characters, which prevent code from executing from a certain point of the line to the end of the line or you can use Block comments. A block comment starts with a slash and an asterisk and end with an asterisk slash. For example, let's say that you wanted to add a comment above the runButton_click function to describe what it does. This would be best served as a block comment. So I'll place the cursor above the function, I'll put in a slash asterisk and then I'll type in some comments, such as this function walks up and down a variable.
Notice that when I press Enter at the end of the line, Visual Web Developer automatically puts in an asterisk. This is a visual trick. If you have a long comment that's a full paragraph, placing the asterisk at the beginning of every line is just a way of visually indicating to the developer that the comment is continuing, but in order to actually complete the comment, you must end with the asterisk slash character. Notice that Visual Web Developer color- codes this sort of comment in black. So that's how you create a block comment. Now here is how you create a beginning of line comment. You can actually do this with a keyboard shortcut. I'm going to select the While loop. Then I'll go to the menu and I'll select Edit > Advanced.
Notice that you can comment and uncomment the selection from the menu. I'll comment the selection with the menu choice. You will see that when you use the menu choice or it's associated keyboard shortcut, you are using beginning of line comments, the double slash combination. Now to uncomment that same code, I'll press Ctrl+K. Notice at the bottom of the Visual Web Developer interface there is a message. (Ctrl+K) was pressed, waiting for second key of chord. A chord is a combination of keyboard shortcuts one after the other. So now I'll press Ctrl+U and that removes the comments.
Ctrl+K, Ctrl+C to add the comments in; Ctrl+K, Ctrl+U to remove them. I will go ahead and leave them in the final stage of this application. I'll save the changes. I'll run the page without debugging and now when I run the code, I'll only see the output from the For loop. I won't see the output for the While loop, because it's commented out. Now, I'll do the same thing in Visual Basic. I'll open the file LoopingVB.aspx.
I'll save the file, naming the new file CommentsVB.aspx. I'll place the cursor above the function runButton_Click. I'll expand to full screen by pressing Alt+Shift+Enter. Now with Visual Basic, you only get beginning of line comments, there is no block comment. So for each line that I would like to comment, I start up with a single quote character. Then I type in the comment. Now if you want to add or remove comments for multiple lines, you can use the same keyboard shortcut that I already demonstrated for C#. I'll select all of the code for the While loop, I'll press Ctrl+K. Notice that Visual Web Developer adds the single quote comment at the beginning of every line. To remove the comment, Ctrl+K, Ctrl+U.
Once again, I'll leave those comments in for the ending state of the file. I'll save my changes and I'll run the page. When the page appears in the browser, I'll run the code. Now I only see the results of the For loop because the While loop is commented out. You are strongly encouraged to comment freely. Use comments to document your code or to temporarily block code from being executed when you are doing your development and testing.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97348 Viewers
61 Video lessons · 84593 Viewers
71 Video lessons · 68801 Viewers
56 Video lessons · 101276 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.