background image

Dim FSO as object

Set FSO = New Scripting.FileSystemObject 

  上面的代码由于在申明的时候没有指定数据类型,在赋值时将浪费内存和

CPU 时

间。正确的代码应该象下面这样:

Dim FSO as New FileSystemObject

3. 尽量避免使用属性

  在平时的代码中,最常见的比较低效的代码就是在可以使用变量的情况下,反复使

用属性(

Property),尤其是在循环中。要知道存取变量的速度是存取属性的速度的 20

倍左右。下面这段代码是很多程序员在程序中会使用到的:

Dim intCon as Integer

For intCon = 0 to Ubound(SomVar())

Text1.Text = Text1.Text & vbcrlf & SomeVar(intCon)

Next intCon 

  下面这段代码的执行速度是上面代码的

20 倍。 

Dim intCon as Integer

Dim sOutput as String

For intCon = 0 to Ubound(SomeVar())

sOutput = sOutput & vbCrlf &

SomeVar(intCon)

Next

Text1.Text = sOutput