【问题标题】:Accessing an object property from within another object that contains it从包含它的另一个对象中访问对象属性
【发布时间】:2023-02-01 01:12:16
【问题描述】:

我会尽量用我有限的知识来解释它...... 我有一个方阵,相同数量的行和列,我需要遍历它并以某种方式存储每个单元格的信息,我以前用数组完成过它并且一切正常,但我正在尝试提高我的编码技能,而且我已经知道对象通常是要走的路有一段时间了......这是我的第二次尝试,因为我被打败了很长一段时间而没有尝试......

问题是……我有 2 个对象,一个叫 Proveedor,这个人将有 3 个属性:
Unidad Decisoria_Prov:一个字符串
Responsable_Prov:一个字符串
Requerimientos:Vinculo 对象数组

Private pedidos_() As Vinculo
Private ud_P As String
Private responsable_P As String
Option Explicit
Public Property Let Requerimientos(index As Integer, str As Vinculo)
    If index > UBound(pedidos_) Then ReDim Preserve pedidos_(index)
    If pedidos_(index) Is Nothing Then
        Set pedidos_(index) = New Vinculo
    End If
    
    Set pedidos_(index) = str
End Property
Public Property Get Requerimientos(index As Integer) As Vinculo
    If index > UBound(pedidos_) Then Requerimientos = "El indice del get esta fuera de rango": Exit Property

    Requerimientos = pedidos_(index)
    
End Property

Public Property Let Unidad_Decisoria_Prov(str As String)
    ud_P = str
End Property
Public Property Get Unidad_Decisoria_Prov() As String
    Unidad_Decisoria_Prov = ud_P
End Property

Public Property Let Responsable_Prov(str As String)
    responsable_P = str
End Property
Public Property Get Responsable_Prov() As String
    Responsable_Prov = responsable_P
End Property

Private Sub class_initialize()
    ReDim pedidos_(0)
End Sub

另一个对象名为 Vinculo,这个对象有 3 个属性:
Unidad Decisoria_C:一个字符串
Responsable_C:一个字符串
要求:一个字符串

Private ud_ As String
Private responsable_ As String
Private requerimiento_ As String
Option Explicit

Public Property Let Unidad_Decisoria_C(str As String)
    ud_ = str
End Property
Public Property Get Unidad_Decisoria_C() As String
    Unidad_Decisoria_C = ud_
End Property

Public Property Let Responsable_C(str As String)
    responsable_ = str
End Property
Public Property Get Responsable_C() As String
    Responsable_C = responsable_
End Property

Public Property Let Requerimiento(str As String)
    requerimiento_ = str
End Property

Public Property Get Requerimiento() As String
    Requerimiento = requerimiento_
End Property

一旦我让对象按预期工作,我将循环遍历矩阵并对数据执行我需要的操作,但在浪费时间之前,我尝试使用以下代码对其进行测试:

Sub testing_2_objetosjuntos()
Dim mi_Prov As Proveedor
Dim un_vin As Vinculo

Set mi_Prov = New Proveedor
Set un_vin = New Vinculo

mi_Prov.Unidad_Decisoria_Prov = "tarea del proveedor 1"
mi_Prov.Responsable_Prov = "responsable de la tarea del proveedor 1"

un_vin.Unidad_Decisoria_C = "tarea del cliente 1"
un_vin.Responsable_C = "responsable de la tarea 1 del cliente"
un_vin.Requerimiento = "hace tal cosa"
mi_Prov.Requerimientos(0) = un_vin

Debug.Print mi_Prov.Requerimientos(0).Requerimiento
Debug.Print mi_Prov.Requerimientos(0).Responsable_C
End Sub

据我所知,根据 debug.print 命令一切正常...... watches up until the first debug.print line

但是,当我尝试访问存储在 Proveedor 对象的第一个索引中的 vinculo 对象的属性时,我得到了一个漂亮的

运行时错误 91: 未设置对象变量或 With 块变量

给我这个错误的实际行是 Proveedor 类中 Requerimientos 的 get 属性。

这可能是一个愚蠢的问题,但不仅我不明白为什么它会崩溃,我显然无法在谷歌中正确地问这个问题而不必打扰你们......

我希望读取存储在 vinculo.Requerimiento 属性中的字符串,该属性位于 Proveedor 对象的 Requerimientos 属性数组的第一个索引中。

【问题讨论】:

  • Set mi_Prov.Requerimientos(0) = un_vin分配对象时需要Set。但是该行与您的 Let 声明不匹配 Requerimientos...

标签: excel vba object oop


【解决方案1】:

您的问题的原因很容易解决。由于Vinculo 是一个对象,您需要在Requerimientos-getter 中使用Set 关键字:

Public Property Get Requerimientos(index As Integer) As Vinculo
    Set Requerimientos = pedidos_(index)
End Property

但是,您的错误处理将失败:如果 index > UBound(pedidos_),则您指定的字符串作为返回值是不允许的(请记住,您的函数返回类型为 Vinculo 的对象,而不是字符串)。
你应该改为 raise an error

Public Property Get Requerimientos(index As Integer) As Vinculo
    Err.Raise vbObjectError + 1000, "El indice del get esta fuera de rango", "Sorry..."
    Set Requerimientos = pedidos_(index)
End Property

还有一个提示:您在识别测试例程的错误时遇到了问题,因为 VBA 运行时没有在错误行上停止。这是因为 VBA 调试器的默认设置是“中断未处理的错误”这不包括在类模块内中断(提示:用户表单也是类)。将此设置更改为“闯入课堂模块”查看发生错误的确切行。

工具->选项->常规

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    相关资源
    最近更新 更多