VBA - 将数组存储在类中 - 类型不匹配

时间:2014-08-19 19:18:01

标签: arrays excel vba class

我一直在寻找,但似乎没有什么可以为我做的。这是问题所在:

我想存储一组“键” 这是我的简单课程:

Private pkeys_length As Integer
Private pkeys() As String
Public Property Get keys_length() As Integer
    keys_length = pkeys_length
End Property
Public Property Let keys_length(arg As Integer)
    pkeys_length = arg
End Property
Public Property Get Keys() As String
    Keys = pkeys()
End Property
Public Property Let Keys(ByVal arg As String)
    ReDim pkeys(0 To pkeys_length) As String
    pkeys = arg
End Property

这就是我想要存储的内容:

Dim str_pkeys() As String
Dim pkey_count As Integer
pkey_count = CountPrimaryKeys(stbl)
'Store the keys of that table
ReDim str_pkeys(pkey_count) As String
keyset_1.keys_length = pkey_count
str_pkeys = FindPrimaryKeys(keyset_1.Table)
keyset_1.Keys = str_pkeys

目前,它给出了错误Compile Error: Type mismatch 我在存放阵列时遇到了一些问题,我不确定我是否真的在任何地方。这是我无法解决的唯一错误。我需要做的就是在类中存储字符串数组。

有谁熟悉如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我认为您需要在Get属性中使用String()并删除Let中的ByVal

Private pkeys_length As Integer
Private pkeys() As String

Public Property Get keys_length() As Integer
    keys_length = pkeys_length
End Property

Public Property Let keys_length(arg As Integer)
    pkeys_length = arg
End Property
Public Property Get Keys() As String()
    Keys = pkeys
End Property

Public Property Let Keys(arg() As String)
    ReDim pkeys(0 To pkeys_length) As String
    pkeys = arg
End Property

除了这个小设计建议:你真的需要一个数组长度的Set吗?为什么不将它包含在数组的集合中 - 并且只提供Get代替?