【问题标题】:Definitions of property procedures for the same property are inconsistent [using arrays]同一属性的属性过程定义不一致[使用数组]
【发布时间】:2016-02-12 23:46:12
【问题描述】:

我遇到了这个讨厌的错误消息(见标题) 我在这个网站和其他网站上的很多帖子上都看到过它,这通常是一些愚蠢的错误,尽管我可能看不到我在做什么愚蠢的事情。

Public Property Get Contents() As Variant
    Contents() = pContents()
End Property

Public Property Let Contents(Values() As Variant)
    pContents = Values()
End Property

Public Property Get Content(Index As Integer) As String
    Content = pContents(Index)
End Property

Public Property Let Content(Index As Integer, Value As String)
...

目的是一对get,让两个允许读取/写入整个数组,第二个允许通过索引读取/写入数组的各个元素。

据此:https://msdn.microsoft.com/en-us/library/office/gg251357.aspx

类模块中的类的让和获取属性声明的东西(是的,我对此很陌生)必须满足某些要求(我很好奇为什么会这样?)。据我所知,我的满足这些要求:

我的 Let 比我的 Get (1) 多了一个参数 (2) - 我在这里假设括号的“as string”outside 不算作参数。 此外,我在 Let 和 Get 中使用相同的类型(整数和字符串),我不需要 Set,所以这应该不是问题。我也尝试过更改参数变量的名称,但无济于事。

所以请帮助我,出了什么问题,我以后如何避免这个错误?

【问题讨论】:

    标签: arrays vba excel class


    【解决方案1】:

    好吧,我似乎已经自己解决了这个问题,在这里发布答案以防它帮助其他人! :)

    将类属性设置为数组(并读取它们)时的注意事项

    1) 小心使用括号():不要在 Let 语句中使用它们

    Public Property Let ArrayProperty (newArray as variant)
    

    相对于诱惑

    Public Property Let ArrayProperty (newArray() as variant) 
    

    同样,在使用此属性时不要使用它们,例如:

    Class1.ArrayProperty = myArray
    

    myArray 后面没有括号

    2) 检查属性 (myArray) Let 语句中的数组是否非空

    在此处链接VBA: Don't go into loop when array is empty 到帮助我完成此任务的答案,看来您必须构建自己的函数或使用错误处理来做到这一点,

    IsEmpty(myArray)
    

    不会工作。

    以及适用于我的目的的代码 sn-p(原始 sn-p 归功于 CreamyEgg):

    Public Function IsEmptyArray(TestArray) As Boolean
    
    Dim lngUboundTest As Long
    
    lngUboundTest = -1
        On Error Resume Next
    lngUboundTest = UBound(TestArray)
    On Error GoTo 0
    
    If lngUboundTest >= 0 Then    
    IsEmptyArray = False
    Else
    IsEmptyArray = True
    End If
    End Function
    

    3) 请记住,您可能需要将属性数组重新调整为 Ubound(newArray),例如

    Public Property Let Contents (newArray as variant)
    'redim pArray to size of proposed array
    ReDim pArray(1 To UBound(newArray))
    'above line will throw an exception if newArray is empty
        pArray = newArray   
    end Property
    

    4) 故意将类属性设为空数组,我使用了一个临时数组变量,在标准模块开头的子外部公开声明

    Public TempArray() As Variant
    'outside of Sub ^^
    
    Sub SetClass1ArrayToEmpty
        Erase TempArray
        class1.ArrayProperty = TempArray
    End Sub
    

    erase 方法会使数组为空,我使用它是因为我偶尔使用 TempArray 来制作大小为 1 的数组并想确保它是空的

    5) 好消息,将范围设置为类属性似乎与将范围设置为数组的工作方式相同,我使用 application.transpose(myRange) 来避免一列出现问题变成二维数组

    class1.ArrayProperty = Application.Transpose(Range(myRange))
    

    你有它,这是工作的类(没有编译错误)

    Public Property Get Contents() As Variant
        Contents() = pContents()
    End Property
    
    Public Property Let Contents(Values As Variant)
    'checks for an empty array being passed to it first
    If IsEmptyArray(Values) Then
        Erase pContents
    Else
    'redim pContents to size of proposed array
        ReDim pContents(1 To UBound(Values))
            pContents = Values
    End If
    End Property
    
    Public Property Get Content(Index As Integer) As String
        Content = pContents(Index)
    End Property
    
    Public Property Let Content(Index As Integer, Value As String)
    
    Select Case Index
        Case Is < 0
            'Error Handling
        Case Is > UBound(pContents) + 1
            'Error Handling
        Case Is = UBound(pContents) + 1  'append to end of array
            ReDim Preserve pContents(UBound(pContents) + 1)
            pContents(Index) = Value
        Case Else                        'replace some middle part of array
            pContents(Index) = Value
    End Select
    
    End Property
    

    希望这可以帮助一些窥视者!

    【讨论】:

    • 谢谢 - 经过大量的谷歌搜索,我终于找到了这个!删除 Let 语句中的 () 帮助了我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 2019-11-13
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多