Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Chapter 2: Visual Basic Programming Concepts
Last change: February 13 2016 18:48:07.
<<PreviousNext>>

Coding Mechanics

You may have noticed that there are two lines of code inside the preceding Button1_Click event procedure:

‘ Make the label display the Welcome message Label1.Text = “Welcome to Visual Basic!!!”

Only the second line causes an action, however. The first line is a comment statement.

Comments on Code

A comment statement starts with a tick (‘) mark or a Rem (for Remark) keyword. When VB sees a line beginning with a tick mark (apostrophe) or Rem, it ignores the line. The following line will have the same effect as the preceding line with a tick mark:

Rem Make the label display the Welcome message

You can also write comments at the end of a code line, as follows:

Label1.Text = “Welcome to Visual Basic!!!” ‘Show Welcome

or

Label1.Text = “Welcome to Visual Basic!!!” Rem Show Welcome

The programmer usually uses comments to provide clues to the purposes of the code. Comments enhance the readability and understandability of the program and are an indispensable part of the program documentation. A well-documented program is much easier to maintain. You should use comments for your program whenever applicable. When you come back to your programs at a later date, you will appreciate your own thoughtfulness if your code is accompanied by plenty of comments.
Furthermore, if you write a program for a company, another programmer may have to update your code in the future. If it is properly commented, the programmer will have a much easier time figuring out what your program does and how it should be changed.

Showing Blocks of Code Lines

The text editor in the IDE that you use to write code will automatically indent the code lines for you, making the program you develop highly readable. You can insert blank lines between any code lines. If your procedure consists of several blocks of code, each dealing with a specific task, inserting blank lines between these blocks can make each block stand out and provide a visual clue to the logical structure of your procedure.

Line Continuation

There may be times when you will have to code a long, complex statement. You may find it desirable to break such a statement into several lines. You can do this by using a space followed by an underscore (_) at end of the line and then continue the remainder of the code in the next line, as shown in the following code.

Label1.Text = _ “Welcome to Visual Basic!!!”

Tip
Do not break a string constant into multiple lines, such as:
Label1.Caption = “Welcome _ To Visual Basic”  
In the first line, the compiler considers the space and the underscore as a part of the string literal; the compiler thus fails to recognize these characters as symbols for line continuation. The correct code should be as follows:
Label1.Caption = “Welcome” _ & “ To Visual Basic”
where the & character is the concatenation operator that joins two strings. String operations are discussed in Chapter 4, “Data, Operations, and Built-in Functions.”

Multiple Statements on a Line

You can have more than one statement on a line by inserting a colon between statements. For example, you can code the following:

HourlyPay = 10 : Area = Height * Width

Although there is nothing wrong with the syntax, you may not want to code your program this way because the statements are harder to read and you can miss the flow of your program logic when reviewing the code.

Last change: February 13 2016 18:48:06.
<<PreviousNext>>