Visual Basic编程的七个优良习惯

来源:岁月联盟 编辑:zhu 时间:2007-02-01
  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

  好处是加快编译速度

 
上一篇:VB能做什么