CST2601 Visual
Basic I
Notes on Code Basics
Code BasicsThis section presents information on code writing mechanics, including breaking and combining lines of code, adding comments to your code, using numbers in code, and following naming conventions in Visual Basic. Breaking a Single Statement Into Multiple LinesYou can break a long statement into multiple lines in the Code window using the line-continuation character (a space followed by an underscore). Using this character can make your code easier to read, both online and when printed. The following code is broken into three lines with line-continuation characters ( _):
You can't follow a line-continuation character with a comment on the same line. There are also some limitations as to where the line-continuation character can be used. Combining Statements on One LineThere is usually one Visual Basic statement to a line, and there is no statement terminator. However, you can place two or more statements on a line if you use a colon (:) to separate them:
In order to make your code more readable, however, it's better to place each statement on a separate line. For More Information For more information, see "Visual Basic Specifications, Limitations, and File Formats." Adding Comments to Your CodeAs you read through the examples in this guide, you'll often come
across the comment symbol (
Comments can follow a statement on the same line or can occupy an entire line. Both are illustrated in the preceding code. Remember that comments can't follow a line-continuation character on the same line. Note You can add or remove comment symbols for a block of code by selecting two or more lines of code and choosing the Comment Block or Uncomment Block buttons on the Edit toolbar. Understanding Numbering SystemsMost numbers in this documentation are decimal (base 10). But occasionally it's convenient to use hexadecimal numbers (base 16) or octal numbers (base 8). Visual Basic represents numbers in hexadecimal with the prefix &H and in octal with &O. The following table shows the same numbers in decimal, octal, and hexadecimal.
You generally don't have to learn the hexadecimal or octal number system yourself because the computer can work with numbers entered in any system. However, some number systems lend themselves to certain tasks, such as using hexadecimals to set the screen and control colors. Naming Conventions in Visual BasicWhile you are writing Visual Basic code, you declare and name many elements (Sub and Function procedures, variables, constants, and so on). The names of the procedures, variables, and constants that you declare in your Visual Basic code must follow these guidelines:
A restricted keyword is a word that Visual Basic uses as part of its language. This includes predefined statements (such as If and Loop), functions (such as Len and Abs), and operators (such as Or and Mod). For More Information For a complete list of keywords, see the Language Reference. Your forms and controls can have the same name as a restricted keyword. For example, you can have a control named Loop. In your code you cannot refer to that control in the usual way, however, because Visual Basic assumes you mean the Loop keyword. For example, this code causes an error:
To refer to a form or control that has the same name as a restricted keyword, you must either qualify it or surround it with square brackets: [ ]. For example, this code does not cause an error:
You can use square brackets in this way when referring to forms and controls, but not when declaring a variable or defining a procedure with the same name as a restricted keyword. Square brackets can also be used to force Visual Basic to accept names provided by other type libraries that conflict with restricted keywords. Note Because typing square brackets can get tedious, you might want to refrain from using restricted keywords as the name of forms and controls. However, you can use this technique if a future version of Visual Basic defines a new keyword that conflicts with an existing form or control name when you update your code to work with the new version. |