Seven good habits of VB programming

zhaozj2021-02-16  49

Seven good habits of VB programming

Sancy Xu (xuqizhi@chinamet.com)

1, "&" Replace " ". In many people's programming languages, use " " to connect the string, which is easy to cause ambiguity. Good habits are "&" to connect strings. Not correct: Dim SMESSAGE AS STRING

SMESSAGE = "1" "2"

Correct: DIM SMESSAGE AS STRING

SMESSAGE = "1" & "2" Note: "&" has a space.

2. Variable name sensation, the statement is wrong with the rank, you compare the following two codes: read the difficulty code: DIM SNAME AS STRINGDIM NTURN AS INTEGER

if NTURN = 0 Thenif Sname = "Sancy" thenend ifdo while until nturn = 4ntrun = NTURN 1LOOPEND IF Easy to read code: DIM SNAME AS STRINGDIM NTURN AS INTEGER

IF NTURN = 0 Then if sname = "Sancy" Then End if do while until nturn = 4 nturn = NTURN 1 LOOPEND IF

3. Under a simple selection condition, use the IIF () function Rosoto code: if nnum = 0 THEN SNAME = "SANcy" else sname = "xu" END IF simple code: SNAME = IIF (nnum = 0, "Sancy", "XU")

4. Try to use Debug.print to debug in many beginners debugging, use MSGBox to track the variable value. In fact, Debug.print can not only achieve the same effect, but also in the final compilation process of the program. And msgbox Must be annotated or deleted. Not correct: msgbox nName correct: debug.pring nname

5. When you repeat the properties of an object, try to use with .... end with

6. Msgbox Try to use the icon in general vbinformation to prompt the message for confirmation or successful operation VBEXCLAMATION to prompt the message vbcritical to prompt the crisis situation message vbquestion to prompt the message to prompt the message

7. The format of the enumeration enumeration is used in possible cases of public enum ... End Enum is the advantage of speeding up the programming speed

转载请注明原文地址:https://www.9cbs.com/read-25920.html

New Post(0)