以下内容是CNrencai网小编通过朋友的经验整理出来的,希望对您有帮助。
1、"&"替换"+"
在很多人的编程语言中,用“+”来连接字符串,这样容易导致歧义。良好的习惯是用“&”来连接字符串.
不正确:
Dim sMessage As String
sMessage = "1" + "2"
正确:
Dim sMessage As String
sMessage = "1" & "2"
注意:"&"的后面有个空格
2、变量命名大小写,语句错落有秩,源代码维护方面
下面大家比较一下以下两段代码:
读懂难度很大的代码:
Dim SNAME As String
Dim NTURN As Integer
If NTURN = 0 Then
If SNAME = "vbeden" Then
Do While NTURN < 4
NTURN = NTURN + 1
Loop
End If
End If
容易读懂的代码:
Dim sName As String
Dim nTurn As Integer
If nTurn = 0 Then
If sName = "vbeden" Then
Do While nTurn < 4
nTurn = nTurn + 1
Loop
End If
End If
3、请养成以下的“对象命名约定”良好习惯
推荐使用的控件前缀
控件类型 前缀 例子
复选框Check box chk chkReadOnly
组合框Combo box cbo cboEnglish
命令按钮Command button cmd cmdExit
通用对话框Common dialog dlg dlgFileOpen
目录列表框Directory list box dir dirSource
驱动器列表框Drive list box drv drvTarget
文件列表框File list box fil filSource
窗体Form frm frmEntry
图象框Image img imgIcon
标签Label lbl lblHelpMessage
列表框List box lst lstPolicyCodes
菜单Menu mnu mnuFileOpen
单选按钮Option button opt optGender
图片框Picture box pic picVGA
文本框Text box txt txtLastName
时钟控件Timer tmr tmrAlarm
变量