Array as a Class Member

后端 未结 3 850
醉话见心
醉话见心 2021-01-02 06:57

I\'m designing a dynamic buffer for outgoing messages. The data structure takes the form of a queue of nodes that have a Byte Array buffer as a member. Unfortunately in VB

3条回答
  •  渐次进展
    2021-01-02 07:37

    Not the most elegant solution, but modeling from the code you provided...

    In clsTest:

    Option Explicit
    
    Dim ArrayStore() As Byte
    
    Public Sub AssignArray(vInput As Variant, Optional lItemNum As Long = -1)
        If Not lItemNum = -1 Then
            ArrayStore(lItemNum) = vInput
        Else
            ArrayStore() = vInput
        End If
    End Sub
    
    Public Function GetArrayValue(lItemNum As Long) As Byte
        GetArrayValue = ArrayStore(lItemNum)
    End Function
    
    Public Function GetWholeArray() As Byte()
        ReDim GetWholeArray(LBound(ArrayStore) To UBound(ArrayStore))
        GetWholeArray = ArrayStore
    End Function
    

    And in mdlMain:

    Sub test()
    Dim buf() As Byte
    Dim bufnew() As Byte
    Dim oBuffer As New clsTest
    
        ReDim buf(0 To 4)
        buf(0) = 1
        buf(1) = 2
        buf(2) = 3
        buf(3) = 4
    
        oBuffer.AssignArray vInput:=buf
        Debug.Print oBuffer.GetArrayValue(lItemNum:=2)
    
        oBuffer.AssignArray vInput:=27, lItemNum:=2
        Debug.Print oBuffer.GetArrayValue(lItemNum:=2)
    
        bufnew() = oBuffer.GetWholeArray
        Debug.Print bufnew(0)
        Debug.Print bufnew(1)
        Debug.Print bufnew(2)
        Debug.Print bufnew(3)
    
    End Sub
    

    I added code to pass the class array to another array to prove accessibility.

    Even though VBA won't allow us to pass arrays as properties, we can still use Functions to pick up where properties fall short.

提交回复
热议问题