Visual Basic編程的七個優良習慣 --提供大家參考 
 
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="sancy" then 
end if 
Do while until NTURN=4 
NTRUN=NTURN+1 
Loop 
End if  
  容易讀懂的代碼: 
dim sName as string 
dim nTurn as integer 
if nTurn=0 then 
 if sName="sancy" then 
 end if 
 Do while until nTurn=4 
  nTurn=nTurn+1 
 Loop 
End if  
  3.在簡單的選擇條件情況下,使用IIf()函數 
  繁瑣的代碼: 
if nNum=0 then 
 sName="sancy" 
else  
 sName="Xu" 
end if  
 
  簡單的代碼: 
sName=IIF(nNum=0,"sancy","Xu")  
  4.儘量使用Debug.print進行調試 
  在很多初學者的調試中,用MsgBox來跟蹤變數值.其實用Debug.print不僅可以達到同樣的功效,而且在程式最後編譯過程中,會被忽略.而MsgBox必須手動注釋或刪除. 
  不正確: 
MsgBox nName  
  正確: 
Debug.pring nName  
  5.在重複對某一物件的內容進行修改時,儘量使用with....end with 
  6.MsgBox中儘量使用圖示 
  一般來說 
  vbInformation用來提示確認或成功操作的消息 
  vbExclamation用來提示警告的消息 
  vbCritical用來提示危機情況的消息 
  vbQuestion用來提示詢問的消息 
  7.在可能的情況下使用枚舉 
  枚舉的格式為 
public enum 
... 
end enum  
  好處是加快編譯速度 |   
 
 
 
 |