-
Notifications
You must be signed in to change notification settings - Fork 2
Basics
If statements get too long, you can break them in a convenient place by placing an underscore at the end of the line prior to pressing Enter.
Dim sVeryLongString As String = "This is an example of a very long string that has been " & _
"split into multiple lines to make the code easier to read."Multiple statements can be combined onto a single line, as in the example below. Notice the three variables are all dimensioned on the same line and, within the For statement, iResult is increased by the value of i and the results are printed to the debug window.
Dim i As Long: Dim iMax As Long = 10: Dim iResult As Long
For i = 1 To iMax
iResult = iResult + i: Debug.Print iResult
Next iComments are created by using the single quote character ('). Anything to the right of the single quote will be ignored by the compiler. Large blocks of text can also be commented using the character combination /* at the beginning and */ at the end. Note: the Wiki engine does not recognize multi-line comments so the text in the multi-line comment below does not look like a comment.
/*
This is an example of a comment that spans
multiple lines. Remember to start the block
with slash-star and end it with star-slash.
*/
'The next line of code creates a variable that will be used later
Dim sMyString as String = "Unknown" 'this is a comment at the end of a line of codeElements in twinBASIC, such as Forms, Subroutines, Functions, Variables, etc. can be named almost anything that makes sense to the developers. However, there are a few rules that must be followed:
- Elements must begin with a letter or an underscore
- Elements must not include the punctuation characters, ! (exclamation point), # (pound sign), % (percent), $ (dollar sign), & (ampersand), @ (at), or . (period).
- Elements must be no longer than 255 characters
- Elements must not be identical to a reserved keyword.
Consistency in naming helps make it easier to maintain your code. People working as part of a team should follow the naming conventions agreed upon by the team. However, the following suggestions have been found useful by Visual Basic and VBA developers for many years:
- Use Hungarian notation for variables, which capitalizes the first character of each separate word, except for the first word. For example, longVariableName.
- Use all captial letters for naming constants and put an underscore between words, e.g., AVOGADADROS_NUMBER.
- Use Pascal case for functions, which is similar to Hungarian notation except with the first character also capitalized. E.g., FindDollarSignPosition.
- All variables and constants should be declared with a type
Private Const AVOGADADROS_NUMBER As Double = 6.022140857 * 10 ^ 23- Use a prefix to identify the scope. A lower-case g can be used to start a global variable and a lower-case m can be used to start a module
- Use a prefix to identify the data type for a variable.
- Boolean: f (lower-case F, which stands for Flag)
- Byte: b (lower-case b)
- Collection: col
- Currency: c (lower-case C)
- Date: dt
- Double: dbl
- Enumeration: e (lower-case e)
- Integer: i (lower-case L)
- Long: l (lower-case I)
- Object: o (lower-case O)
- Single: sng
- String: s (lower-case S)
- Variant: v (lower-case V)
- User-Interface Elements
- CheckBox: chk
- ComboBox: cbo
- CommandButton: cmd
- Form: frm
- Interface: I
- Label: lbl
- ListBox: lst
- OptionButton: opt
- Report: rpt
- ScrollBar: hsb for horizontal and vsb for vertical scrollbars
- Textbox: txt
- TabControl: tab
- Loop Variables
- Use a lower-case i for first-level loops
- Use a lower-case j for second-level loops, or loops inside another loop
- Use a lower-case k for third-level loops, or consider restructuring your code to not require looping three or more levels deep
- Only return basic native types from functions. Instead of returning an object or array, consider passing it into the function ByRef and returning a boolean that represents whether the function completed successfully.
- Avoid using error handlers as part of your program flow. For example, instead of relying on an error handler to catch an object that did not get instantiated correctly, test to make sure it was instantiated correctly prior to attempting to use it.
- Avoid raising errors using Err.Raise. Instead, use functions that return a boolean or enumeration that indicates success or failure.