数组作为类成员 [英] Array as a Class Member

查看:578
本文介绍了数组作为类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了传出消息的动态缓冲区。该数据结构可具有一个字节数组缓冲液作为成员节点的一个队列的形式。不幸的是在VBA中,数组不能是一个类的公共成员。

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 VBA, Arrays cannot be public members of a class.

例如,这是一个没有没有,不会编译:

For example, this is a no-no and will not compile:

'clsTest

Public Buffer() As Byte

您会收到以下错误:常量,定长字符串,数组,用户自定义类型和声明不允许作为对象模块的公共成员声明

You will get the following error: "Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules"

好吧,这很好,我就使其与公众属性访问私有成员...

Well, that's fine, I'll just make it a private member with public Property accessors...

'clsTest

Private m_Buffer() As Byte

Public Property Let Buffer(buf() As Byte)
    m_Buffer = buf
End Property

Public Property Get Buffer() As Byte()
    Buffer = m_Buffer
End Property

...然后一个模块中的一些测试,以确保它的工作原理:

...and then a few tests in a module to make sure it works:

'mdlMain

Public Sub Main()
    Dim buf() As Byte
    ReDim buf(0 To 4)

    buf(0) = 1
    buf(1) = 2
    buf(2) = 3
    buf(3) = 4


    Dim oBuffer As clsTest
    Set oBuffer = New clsTest

    'Test #1, the assignment
    oBuffer.Buffer = buf    'Success!

    'Test #2, get the value of an index in the array
'    Debug.Print oBuffer.Buffer(2)   'Fail
    Debug.Print oBuffer.Buffer()(2)    'Success!  This is from GSerg's comment

    'Test #3, change the value of an index in the array and verify that it is actually modified
    oBuffer.Buffer()(2) = 27
    Debug.Print oBuffer.Buffer()(2)  'Fail, diplays "3" in the immediate window
End Sub

测试#1做工精细,<删除>,但测试#2断裂,缓存被突出显示,并且错误消息的参数或无效的属性赋值打错了

Test #1 works fine, but Test #2 breaks, Buffer is highlighted, and the error message is "Wrong number of arguments or invalid property assignment"

测试#2现在的作品! GSerg指出,为了调用属性Get缓冲器()正确,也指一个特定的索引缓冲区中,两个套​​括号是必要的: oBuffer.Buffer()(2)

Test #2 now works! GSerg points out that in order to call the Property Get Buffer() correctly and also refer to a specific index in the buffer, TWO sets of parenthesis are necessary: oBuffer.Buffer()(2)

测试#3失败 - 3原值被打印到立即窗口。 GSerg在他的评论,即在公共财产获取缓冲器()只返回一个拷贝,而不是实际的类成员数组指出,这样的修改都将丢失。

Test #3 fails - the original value of 3 is printed to the Immediate window. GSerg pointed out in his comment that the Public Property Get Buffer() only returns a copy and not the actual class member array, so modifications are lost.

如何可以在这个第三个问题得到解决使类成员阵列的工作如预期?

(我要澄清的是,一般的问题是VBA不允许数组为类的公共成员。我怎样才能解决这个问题有这种行为,如果它是一类的数组成员所有的实际目的,包括:1分配阵列,#2正从数组值,#3指派阵列和#4中值直接使用数组中调用 CopyMemory的(#3和#4是几乎相等)?)

(I should clarify that the general question is "VBA doesn't allow arrays to be public members of classes. How can I get around this to have an array member of a class that behaves as if it was for all practical purposes including: #1 assigning the array, #2 getting values from the array, #3 assigning values in the array and #4 using the array directly in a call to CopyMemory (#3 and #4 are nearly equivalent)?)"

推荐答案

因此​​,原来我需要从OLEAUT32.DLL一点帮助,特别是的VariantCopy'功能。此功能使得如实一个变体的精确拷贝到另一个的包括当它的ByRef!

So it turns out I needed a little help from OleAut32.dll, specifically the 'VariantCopy' function. This function faithfully makes an exact copy of one Variant to another, including when it is ByRef!

'clsTest

Private Declare Sub VariantCopy Lib "OleAut32" (pvarDest As Any, pvargSrc As Any)

Private m_Buffer() As Byte

Public Property Let Buffer(buf As Variant)
    m_Buffer = buf
End Property

Public Property Get Buffer() As Variant
    Buffer = GetByRefVariant(m_Buffer)
End Property

Private Function GetByRefVariant(ByRef var As Variant) As Variant
    VariantCopy GetByRefVariant, var
End Function

有了这个新的定义,所有的测试都通过了!

With this new definition, all the tests pass!

'mdlMain

Public Sub Main()
    Dim buf() As Byte
    ReDim buf(0 To 4)

    buf(0) = 1
    buf(1) = 2
    buf(2) = 3
    buf(3) = 4


    Dim oBuffer As clsTest
    Set oBuffer = New clsTest

    'Test #1, the assignment
    oBuffer.Buffer = buf    'Success!

    'Test #2, get the value of an index in the array
    Debug.Print oBuffer.Buffer()(2)    'Success!  This is from GSerg's comment on the question

    'Test #3, change the value of an index in the array and verify that it is actually modified
    oBuffer.Buffer()(2) = 27
    Debug.Print oBuffer.Buffer()(2)  'Success! Diplays "27" in the immediate window
End Sub

这篇关于数组作为类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆