ENHANCING YOUR CODE WITH COMMENTS


As your Visual Basic applications get bigger and more complicated, so will the number and complexity of the programming statements that you add to them. As the amount of code in an application grows, it becomes increasingly hard to follow along. Program code that makes perfect sense to you as you write it today may be very difficult for you to come back to a year or two later and try to work with again. It can be especially difficult for someone else who may have to follow behind you and make modifications or enhancements to the application.

So, like Hansel and Gretel, to make things easier on yourself or on someone who may have to follow up behind you, it's a good idea to leave a trail of breadcrumbs. You can leave this trail in the form of comments. By embedding comments at key points in your applications, you can leave behind notes that explain why you did things the way you did.

In Visual Basic, comments start with the ' character, as demonstrated below.

 'Define an integer type variable named intCounter Dim intCounter As Integer = 0 

image from book
DEFINITION

A comment is a statement embedded within an application that contains a text string that is visible to the programmer but ignored by the application.

image from book

Trick 

You can also embed comments within your Visual Basic applications using the REM keyword. REM, which stands for remark, Is a holdover from the earliest version of the BASIC programming language. Although still supported, you won't see it used by professional programmers.

As you can see, the first statement in the previous example documents what the following statement does. When executed, Visual Basic ignores the first statement, only executing the second line of code. Visual Basic also allows you to add comments to the end of other statements, as demonstrated below.

 MessageBox.Show("The date is: " & Now())   'Display current date and time 

Trick 

Another good use of comments is to temporarily comment out statements when testing your application at development time. For example, you might develop an application that displays a message every time the application starts up, as demonstrated below.

 Private Sub Form1_Load(ByVal sender As System.Object,   ByVal e As System.EventArgs) Handles MyBase.Load         MessageBox.Show("Welcome to my application!") End Sub 

Seeing the pop-up window displayed by the MessageBox.Show method appear every time you test the execution of your application may get tiresome after a while, so to speed up your testing, you might choose to comment out the MessageBox.Show statement while you are working on your application. Once you are done making changes, all you have to do is remove the comment, and your application will resume displaying the pop-up dialog whenever it is loaded.




Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
ISBN: 1592008143
EAN: 2147483647
Year: 2006
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net