在VBA中使用父属性和数组时遇到错误

我得到VBA中的运行时错误91(对象variables或With块未设置)。

我正在尝试访问我构build的父类的属性,但我得到了错误91.我可以访问属性而不使用父属性,但是当我使用父属性时,我得到一个错误。

我有4个class(员工,员工,工程师和工程师)。 员工和工程师分别拥有一个员工和工程师的集合(我没有在这里使用集合对象,我只是使用一个简单的数组)。

我的员工类。

Option Explicit Private objEngineers As Engineers Private objEngineer As Engineer Private varCode As Variant Private Sub Class_Initialize() Set objEngineer = New Engineer Set objEngineer.Parent = Me End Sub Public Property Get Engineers() As Engineers Set Engineers = objEngineers End Property Public Property Set Engineers(objmEngineers As Engineers) Set objEngineers = objmEngineers End Property Public Property Get Code() As Variant Code = varCode End Property Public Property Let Code(varmCode As Variant) varCode = varmCode End Property 

员工类

 Option Explicit Private objEmployee() As Employee Sub Add() Dim i As Integer Dim objEngineers As Engineers Set objEngineers = New Engineers objEngineers.Add ReDim objEmployee(1 To 3) For i = 1 To 3 Set objEmployee(i) = New Employee objEmployee(i).Code = "Employee " & i Set objEmployee(i).Engineers = objEngineers Next End Sub Property Get Item(i As Integer) As Employee Set Item = objEmployee(i) End Property 

工程师class

  Option Explicit Private objEmployee As Employee Private varID As Variant Property Get Parent() As Employee Set Parent = objEmployee End Property Property Set Parent(objmEmployee As Employee) Set objEmployee = objmEmployee End Property Public Property Get ID() As Variant ID = varID End Property Public Property Let ID(varmID As Variant) varID = varmID End Property 

工程师class

  Option Explicit Private objEngineer() As Engineer Private objEmployee As Employee Sub Add() Dim i As Integer ReDim objEngineer(1 To 3) For i = 1 To 3 Set objEngineer(i) = New Engineer objEngineer(i).ID = "Engineer " & i Next End Sub Property Get Item(i As Integer) As Engineer Set Item = objEngineer(i) End Property 

最后是我的testing模块

  Option Explicit Sub Main() Dim objEmployees As Employees Dim i As Integer Set objEmployees = New Employees objEmployees.Add For i = 1 To 3 Debug.Print objEmployees.Item(i).Engineers.Item(i).ID Debug.Print objEmployees.Item(i).Code Debug.Print objEmployees.Item(i).Engineers.Item(i).Parent.Code Next End Sub 

我可以使循环中的第一行工作“工程师1”。 此外,给出“员工1”的第二行工作,但第三行应该给我与第二行相同的结果,我得到的错误。 当我使用工程师的Parent属性时,我无法访问Employee的属性。

我知道我错过了一些明显的东西,我找不到它。

基于迪克Kusleika 这个答案

蒸馏到足以certificate一个父母的财产

类模块ParentClass

 Option Explicit Private pName As String Private pChild As ChildClass Public Property Let Name(NewName As String) pName = NewName End Property Public Property Get Name() As String Name = pName End Property Public Property Get Child() As ChildClass Set Child = pChild End Property Public Sub Add(NewName As String) Set pChild = New ChildClass pChild.Name = NewName Set pChild.Parent = Me End Sub 

类模块ChildClass

 Option Explicit Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ (dest As Any, Source As Any, ByVal bytes As Long) Private pName As String Private pParentPtr As Long Public Property Let Name(NewName As String) pName = NewName End Property Public Property Get Name() As String Name = pName End Property Public Property Get Parent() As ParentClass Set Parent = ObjFromPtr(pParentPtr) End Property Friend Property Set Parent(Obj As ParentClass) pParentPtr = ObjPtr(Obj) End Property Private Function ObjFromPtr(ByVal pObj As Long) As Object Dim Obj As Object CopyMemory Obj, pObj, 4 Set ObjFromPtr = Obj ' manually destroy the temporary object variable ' (if you omit this step you'll get a GPF!) CopyMemory Obj, 0&, 4 End Function 

用于演示目的的标准模块

 Sub Demo() Dim EgParent As ParentClass Dim EgChild As ChildClass Set EgParent = New ParentClass EgParent.Name = "I am the Parent" EgParent.Add "I am the Child" Set EgChild = EgParent.Child Debug.Print EgParent.Name Debug.Print EgChild.Name Debug.Print EgChild.Parent.Name End Sub