在VB6中,字符串在必要时可以与字符数组互相赋值,二者在很多时候等效,使用起来非常方便。
例如:
dim b() as byte, Str as string
Str = "A string."
b() = Str
Str = b
Debug.Print b(), Str
这里的b()可加括号,也可以不加括号。那么,这两个有什么区别呢?
请看下面msdn里的例子(原例有错误,已修改)。
Private Sub Form_Load()
Dim b As Byte
Dim I As Integer
Dim ReturnArray() As Byte
b = CByte(54)
ReturnArray() = ArrayFunction(b)
For I = 0 To UBound(ReturnArray)
Debug.Print ReturnArray(I)
Next
End Sub
Public Function ArrayFunction(b As Byte) As Byte()
Dim x(2) As Byte
x(0) = b
x(1) = b + CByte(200)
x(2) = b + b
ArrayFunction = x
End Function
微软在例子后面的说明是:
注意,Exit Function 语句将一个数组作为参数传递;且数组的数据类型必须和函数的数据类型相同(在本例中是字节)。因为这是一个函数调用,传递数组时不必带括号。
注意 尽管可以通过赋值给另一个数组(ArrayFunction = x())来返回一个数组,但出于性能方面的考虑,并不推荐使用这种方法。
可见,带括号,是将之作为数组处理,一个个值处理,效率低。不带括号,是作为函数调用整体处理,效率高。
2989

被折叠的 条评论
为什么被折叠?



