类模块数组属性 ByRef Variant Index
设置数组属性(ByRef),其中修改了传递的参数值,为何不能够改变被传址数组的值
如何使类的属性既能传递给数组,又能传递给数组的一个元素?
Class1.cls
Option Explicit
Private B() As Byte
Public Property Get mArray() As Byte() 'Optional bIndex As Variant
'If IsMissing(bIndex) Then
Let mArray = B()
'Else
'mArray = b(bIndex)
'End If
End Property
Rem 如何既能传递数组,又能传递数组的一个元素
Public Property Let mArray(ByRef vData() As Byte) 'Optional bIndex As Variant
'If IsMissing(bIndex) Then
Erase B()
Let B() = vData()
'Else
'b(bIndex) = vData(vIndex)
'End If
On Error GoTo ErrCancel:
If CStr((LBound(vData()))) <> "" Then
vData(LBound(vData())) = 123 '测试 Byref 是否能够改变被传址数组的值
End If
Stop
Exit Property
ErrCancel:
On Error GoTo 0
End Property
Public Property Let Test(ByRef mString As String, mStart As Long, Optional mLength As Variant, vData As String)
If IsMissing(mLength) Then
Mid(mString, mStart) = vData
Else
Mid(mString, mStart, mLength) = vData
End If
End Property
Public Property Get Test(mString As String, mStart As Long, Optional mLength As Variant) As String
If IsMissing(mLength) Then
Test = Mid(mString, mStart)
Else
Test = Mid(mString, mStart, mLength)
End If
End Property
Option Explicit
Private Sub Command1_Click()
Dim Cls1 As Class1
Set Cls1 = New Class1
Dim B() As Byte
ReDim B(30) As Byte
Rem 测试1
Dim S As String
S = "abcdefg"
Cls1.Test(S, 1, 3) = "12"
Debug.Print Cls1.Test(S, 1, 3) 'Byref 起到作用
'__________________________________________
Debug.Print
Rem 测试2
B(LBound(B())) = 234
Let B() = Cls1.mArray()
On Error Resume Next
Debug.Print B(LBound(B())) 'Cause an error
ReDim B(10) As Byte
Let Cls1.mArray() = B() 'Byref 没有起到作用
Debug.Print B(LBound(B()))
Stop
End Sub