设置数组属性(ByRef),其中修改了传递的参数值,为何不能够改变被传址数组的值
如何使类的属性既能传递给数组,又能传递给数组的一个元素?
Class1.cls
Option ExplicitPrivate B() As BytePublic Property Get mArray() As Byte()               'Optional bIndex As Variant
  'If IsMissing(bIndex) Then
    Let mArray = B()
  'Else
    'mArray = b(bIndex)
  'End If
End PropertyRem                                                  如何既能传递数组,又能传递数组的一个元素
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 PropertyPublic 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
Form1.frm
Option ExplicitPrivate 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

解决方案 »

  1.   

    Property Let 没办法
    只要你用 Property
    因为即使你设定 ByRef 
    传进去都会被自动转为 ByVal
      

  2.   

    我有用 VB 写 DLL 给 VC 调用
    ByRef 传过去给 VC 时
    VC 那边的 type def 看到的好像变成是 ByVal
      

  3.   

    可是有时后ByRef不会转换成ByVal啊。
      

  4.   

    講白一點雖然你定義 Public Property Let mArray(ByRef vData() As Byte) 但是 VB 會自動把它變為 Public Property Let mArray(ByVal vData() As Byte) 
      

  5.   

    对啊只有 Property Let 类属性中才会这样 只能用 Sub 或 Function ( ) 才有办法用 ByRef 喔
      

  6.   

    那不是数组也要放到 Sub 或 Function ( )中吗?
      

  7.   

    Public Property Get mArray() As Byte()
        Let mArray = B()
    End Property
    Public Sub Let_mArray(vData() As Byte)
        Erase B()
        B() = vData()
        vData(LBound(vData())) = 123
    End Sub
      

  8.   

    就是把 Public Property Let mArray(ByRef vData() As Byte)换成Public Sub Let_mArray(vData() As Byte)那么 vData(LBound(vData())) = 123  数值就会修改成功
      

  9.   

    '=========== class1 ==========Dim B() As BytePublic Property Get mArray() As Byte()
        Let mArray = B()
    End Property
    Public Sub Let_mArray(vData() As Byte)
        Erase B()
        B() = vData()
        vData(LBound(vData())) = 123
    End Sub'============= Form ================Private Sub Form_Load()
      Dim c As New Class1Dim QQ(11) As Byte
      c.Let_mArray QQ()
      MsgBox QQ(LBound(QQ))
    End Sub
      

  10.   

    怎么变成方法了,不过这样确实可以,但是属性变成只读了。你说一下 Property Let 类属性中,ByRef 参数是数组和不是数组,在VC++代码中的区别。
      

  11.   

    Property Let 与 Functiont 和 Sub 不同之处就
    1是没有返回值
    2是只能用于属性表达式或Let语句的左边也就是你示例代码中:Let Cls1.mArray() = B() 这一行,对表达式右边的数组B没有作用,也就是B不作为返回值返回你的修改...
    作为参数,数组缺省就是ByRef,但属性的参数(你代码中的vData)是由赋值表达式的右边传递的,这就是与 Functiont 和 Sub 的参数不同之处...所以只能说你代码中第二个Property Let似是而非,改变的并非与属性相关的vData....
      

  12.   

    你这是写的什么呀。
    很简单的:Public Property Get mArray() As Byte()           
        mArray = B
    End PropertyPublic Property Let mArray(vData() As Byte) 
      b=vdata
    End Property'给单个元素赋值
    Public Property Let item(index as long, v as byte) 
      b(index)=v
    End Property