【问题标题】:VBA - Store an array inside of a class - Type mismatchVBA - 在类中存储数组 - 类型不匹配
【发布时间】:2014-08-19 19:18:00
【问题描述】:

我一直在寻找,但似乎没有什么对我有用。问题来了:

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

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 我在存储数组时遇到了几个问题,我不确定我是否真的到了任何地方。这是我无法修复的唯一错误。我需要做的就是将字符串数组存储在类中。

有谁知道如何处理这个问题?

【问题讨论】:

    标签: arrays excel vba class


    【解决方案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
    

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

    【讨论】:

    • 谢谢你,它成功了!而且我只是在摆弄不同的特性——VBA 中的类——永远学不完;)。我将如何将它包含在数组的集合中?再次感谢彼得。
    • Public Property Let Keys(arg() As String) ReDim pkeys(0 To pkeys_length) As String pkeys = arg pkeys_length = FunctionToDetermineLenghthLikeUBoundHere(arg) End Property
    猜你喜欢
    • 2013-05-26
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多