Except for global variables (which should not be passed), procedures and functions should operate only on objects passed to them. The global variables used in the process should be identified in the declaration section at the beginning of the process. In addition, the parameters should be passed to the Sub process and function process using ByVal, unless it is obvious that the passed parameter value needs to be changed.
As the size of the project grows, the work of dividing the range of variables also increases rapidly. This growth is indicated by placing a single-letter range prefix in front of the type prefix, but the length of the variable name does not increase much.
Variable range prefix
Range Prefix Example
Global g gstrUserName
Module level m mblnCalcInProgress
Local to process None dblVelocity
If a variable is declared as Public in a standard module or form module, then the variable has a global scope. If a variable is declared as Private in a standard module or form module, then the variable has a module-level scope.
Note: Consistency is the key to using this technology effectively; the syntax checker in Visual Basic does not capture module-level variables starting with "p."
constant
The body of a constant name is a case-mixed and the first letter of each word is capitalized. Although standard Visual Basic constants do not contain data type and range information, prefixes such as i, s, g, and m are still useful for understanding the values and ranges of a constant. For constant names, the same rules as the variable should be followed. For example:
mintUserListMax 'Maximum limit on user list
'(Integer value, local to module)
gstrNewLine 'Newline Character
'(String, global use of the application)
variable
Declaring all variables will save programming time because the errors caused by typing are reduced (for example, whether it is aUserNameTmp, sUserNameTmp, or sUserNameTemp). In the Options dialog box, in the Editor tab, check the Require variable declaration option. The Option Explicit statement requires that all variables be declared in the Visual Basic program.
Variables should be prefixed to indicate their data type. And the prefix can be extended to indicate the range of variables, especially for large programs.
Use the following prefix to indicate the data type of a variable.
Variable data type
Data type Prefix Example
String (String type) str strFName
Integer (short integer type) int intQuantity
Long (Long integer type) lng lngDistance
Single (single precision floating point number type) sng sngAverage
Double (Double precision floating point number type) dbl dblTolerance
Boolean (Bolean type) bln blnFound
Byte (byte type) byt bytRasterData
Date (date type) dte dteNow
Currency (Currency calculation and fixed-point calculation types) cur curRevenue
Object (Object type) obj objCurrent
Variant (Variation type) vnt vntCheckSum
Describe variables and process names
The body of a variable or process name should be in a case mix and should be long enough to describe what it does. Moreover, the function name should start with a verb, such as InitNameArray or CloseDialog.
For frequently used or long terms, standard abbreviations are recommended to rationalize the length of the name. Generally speaking, variable names with more than 32 characters are difficult to read on a VGA monitor.
When using abbreviations, make sure they are consistent throughout the application. In a project, if you use Cnt and Count at a moment, it will lead to unnecessary confusion.
User-defined types
In a large project with many user-defined types, it is often necessary to give each type a three-character prefix. If these prefixes start with "u", it is easy to quickly identify these types when working with a user-defined type. For example, ucli can be used as a prefix for a user-defined client type variable.
[Back to index]
4. Under simple selection conditions, use the IIf() function
Rosso's code:
If nNum = 0 Then
sName = "sancy"
Else
sName = "Xu"
End If
Simple code:
sName=IIf(nNum=0,"sancy","Xu")
5. Try to use it for debugging
In the debugging of many beginners, MsgBox is used to track variable values. In fact, it can not only achieve the same effect, but will also be ignored during the final compilation of the program. MsgBox must be manually commented or deleted.
generally:
MsgBox nName
should:
nName
6. When repeatedly modifying the properties of a certain object, try to use With...End With
generally:
= 5000
= 6000
= "This is MyLabel"
should:
With Form1
.Height = 5000
.Width = 6000
.Caption = "This is MyLabel"
End With
This kind of structure program execution efficiency is relatively high, especially in loop statements.
7. Try to use the message icon in MsgBox, so that the program is more standardized
Generally speaking
vbInformation is used to prompt for confirmation or successful operation
vbExclamation message used to prompt warning
vbCritical information used to prompt the crisis situation
vbQuestion message used to prompt for inquiries
[Back to index]
8. Use enums where possible
The format of the enum is
[Public | Private] Enum name
membername [= constantexpression]
membername [= constantexpression]
....
End Enum
The Enum statement contains the following parts:
Part Description
Public Optional. Indicates that the Enum type is visible throughout the project. The default situation of Enum type is Public.
Private Optional. Indicates that the Enum type is only visible in the declared module.
name Required. The name of the Enum type. name must be a legal Visual Basic identifier, and the type is specified using this name when defining a variable or parameter of the Enum type.
membername Required. A legal Visual Basic identifier used to specify the name of the constituent element of the Enum type.
constantexpression optional. The value of the element (of type Long). It can be another Enum type. If a constantexpression is not specified, the assigned value is either 0 (if the element is the first membername), or 1 is greater than its direct predecessor.
illustrate
The so-called enum variable refers to a variable defined by the Enum type. Variables and parameters can be defined as Enum type. Elements in the Enum type are initialized as constant values specified in the Enum statement. The assigned value may include positive and negative numbers and cannot be changed at runtime. For example:
Enum SecurityLevel IllegalEntry = -1 SecurityLevel1 = 0 SecurityLevel2 = 1 End Enum
The Enum statement can only appear at the module level. After defining the Enum type, you can use it to define variables, parameters, or process to return that type. The Enum type cannot be qualified by module name. The Public Enum types in the class module are not members of the class; they are also written to the type library. The Enum type defined in the standard module is not written into the type library. Public Enum types with the same name cannot be defined both in standard modules and in class modules because they share the same namespace. If two Enum types in different type libraries have the same name but different members, the reference to variables of this type will depend on which type library has higher reference priority.
The Enum type cannot be used as the target in the With block.
Enum statement example
The following example demonstrates the use of the Enum statement to define a set of named constants. In this example, some selectable color constants are used to design the data input form for the database.
Public Enum InterfaceColors
icMistyRose = &HE1E4FF&
icSlateGray = &H908070&
icDodgerBlue = &HFF901E&
icDeepSkyBlue = &HFFBF00&
icSpringGreen = &H7FFF00&
icForestGreen = &H228B22&
icGoldenrod = &H20A5DA&
icFirebrick = &H2222B2&
End Enum
The advantage is to speed up programming