【问题标题】:Creating Property Get/Let procedures for a private array为私有数组创建 Property Get/Let 过程
【发布时间】:2015-03-29 12:25:46
【问题描述】:

我正在尝试使用公共 Property Get/Let 过程将多个值分配给类模块中的私有数组。但是当我尝试获取值时,数组显示为空。这是为什么呢?

以下是相关代码:

Private pdt_RentStepDate(23) As Date

Public Property Get dt_RentStepDate(ByRef d1 As Integer) As Date
    dt_RentStepDate(d1) = pdt_RentStepDate(d1)
End Property

Public Property Let dt_RentStepDate(ByRef d1 As Integer, something As Date)
    pdt_RentStepDate(d1) = something
End Property

【问题讨论】:

  • 您在代码中使用Option Explicit 吗?我想知道,因为我同时看到了 pd_RentStepRatepdt_RentStepDate 变量:msdn.microsoft.com/en-us/library/office/…
  • 顺便说一句,这也是它“跳到让”的原因。您已经告诉您要查看 Let,而不是您的私有模块级变量。
  • 好的,看来您已根据我在我现在已删除的答案中的评论更新了问题。您的属性定义现在对我来说看起来很合理。下一个问题是如何调用类和属性。您需要出示该代码才能获得答案。

标签: arrays vba properties


【解决方案1】:

在您的 Property Get 过程中,分配返回值时不要指定索引:

Public Property Get dt_RentStepDate(ByRef d1 As Integer) As Date
'   dt_RentStepDate(d1) = pdt_RentStepDate(d1)  ' <-- WRONG
    dt_RentStepDate = pdt_RentStepDate(d1)      ' <-- RIGHT
End Property

如果您指定一个索引,那么您实际上是使用参数d1pdt_RentStepDate(d1) 调用Property Let,而不是为Property Get 分配返回值。 (这不起作用,因为 Property Let 最终将 pdt_RentStepDate(d1) 设置为自身。)由于没有分配返回值,因此 Property Get 总是返回一个空的 Date。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 2013-06-11
    • 2022-10-05
    • 2011-06-29
    • 2014-03-31
    • 2021-07-05
    相关资源
    最近更新 更多