弱电初学者 发表于 2007-4-14 12:28:50

Visual Basic编程的七个优良习惯

<SPAN class=f14>  1、"&"替换"+".<BR><BR>  在很多人的编程语言中,用“+”来连接字符串,这样容易导致歧义。良好的习惯是用“&”来连接字符串.<BR><BR>  不正确:<BR><BR><TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf border=1><TBODY><TR><TD>dim sMessage as string <BR>sMessage="1"+"2"</TD></TR></TBODY></TABLE><BR>  正确:<BR><BR><TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf border=1><TBODY><TR><TD>dim sMessage as string<BR><BR>sMessage="1" & "2"</TD></TR></TBODY></TABLE><BR>  注意:"&"的后面有个空格.<BR><BR>  2.变量命名大小写,语句错落有秩<BR><BR>  下面大家比较一下以下两段代码:<BR><BR>  读懂难度很大的代码:<BR><BR><TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf border=1><TBODY><TR><TD>dim SNAME as string<BR>dim NTURN as integer<BR><BR><BR>if NTURN=0 then<BR>if SNAME="sancy" then<BR>end if<BR>Do while until NTURN=4<BR>NTRUN=NTURN+1<BR>Loop<BR>End if</TD></TR></TBODY></TABLE><BR>  容易读懂的代码:<BR><BR><TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf border=1><TBODY><TR><TD>dim sName as string<BR>dim nTurn as integer<BR><BR>if nTurn=0 then<BR><BR> if sName="sancy" then<BR><BR> end if<BR><BR> Do while until nTurn=4<BR>  nTurn=nTurn+1<BR> Loop<BR>End if</TD></TR></TBODY></TABLE><BR>  3.在简单的选择条件情况下,使用IIf()函数<BR><BR>  繁琐的代码:<BR><BR><TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf border=1><TBODY><TR><TD>if nNum=0 then<BR> sName="sancy"<BR>else <BR> sName="Xu"<BR>end if</TD></TR></TBODY></TABLE><BR><BR>  简单的代码:<BR><BR><TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf border=1><TBODY><TR><TD>sName=IIF(nNum=0,"sancy","Xu")</TD></TR></TBODY></TABLE><BR>  4.尽量使用Debug.print进行调试<BR><BR>  在很多初学者的调试中,用MsgBox来跟踪变量值.其实用Debug.print不仅可以达到同样的功效,而且在程序最后编译过程中,会被忽略.而MsgBox必须手动注释或删除.<BR><BR>  不正确:<BR><BR><TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf border=1><TBODY><TR><TD>MsgBox nName</TD></TR></TBODY></TABLE><BR>  正确:<BR><BR><TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf border=1><TBODY><TR><TD>Debug.pring nName</TD></TR></TBODY></TABLE><BR>  5.在重复对某一对象的属性进行修改时,尽量使用with....end with<BR><BR>  6.MsgBox中尽量使用图标<BR><BR>  一般来说<BR><BR>  vbInformation用来提示确认或成功操作的消息<BR>  vbExclamation用来提示警告的消息<BR>  vbCritical用来提示危机情况的消息<BR>  vbQuestion用来提示询问的消息<BR><BR>  7.在可能的情况下使用枚举<BR><BR>  枚举的格式为<BR><BR><TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf border=1><TBODY><TR><TD>public enum<BR>...<BR>end enum</TD></TR></TBODY></TABLE><BR>  好处是加快编译速度</SPAN>
页: [1]
查看完整版本: Visual Basic编程的七个优良习惯