问题
I found a great post on SO that seems to be exactly what I want: Is it possible to access a parent property from a child that is in a collection? However my adaptation of it is giving me Object doesn't support this property or method.
My code which now works thanks to Mat's Mug and Tomalak:
Parent Class - clsComputer
Option Explicit
Private pCD As clsCD
''''''''''''''''''''''''''''''
' CD property
''''''''''''''''''''''''''''''
Public Property Get CD() As clsCD
If pCD Is Nothing Then
Set pCD = New clsCD
'Per Mat's Mug post, drop the parenthesis
pCD.Initialze Me
End If
Set CD = pCD
End Property
Public Property Set CD(value As clsCD)
pCD = value
End Property
Child class - clsCD
Option Explicit
Private pParent As clsComputer
'''''''''''''''''''''''''''''
' Status property - READ ONLY
'''''''''''''''''''''''''''''
Public Property Get Status(Optional strHost As String) As String
Dim strResult As String
If strHost = "" Then strHost = Me.Parent.HostName
strResult = RunCMD("cmd /c ""winrs -r:" & strHost & _
" reg query hklm\system\currentcontrolset\services\cdrom /v start""")
If InStr(1, strResult, "0x4", vbTextCompare) Then
Status = "Disabled"
Else
Status = "Enabled"
End If
End Property
'''''''''''''''''''''''''
' Parent property
'''''''''''''''''''''''''
Public Property Get Parent() As clsComputer
Set Parent = pParent
End Property
'Because as Tomalak points out, you use Set with Objects.
Public Property Set Parent(Obj As clsComputer)
Set pParent = Obj
End Property
'''''''''''''''''''''''''
' Initialize Method
'''''''''''''''''''''''''
Public Sub Initialize(Obj As clsComputer)
Set Me.Parent = Obj
End Sub
Code Module - Module1
Sub test()
Dim oPC As clsComputer
Set oPC = New clsComputer
Debug.Print "CD Status: " & oPC.CD.Status
End Sub
If I test Me, it is an object (eg, If IsObject(Me) Then Stop
evaluates true), and Intellisense shows all the properties and methods in clsComputer when I type Me.
The Locals windows shows Me as a clsComputer object. Everything I know to check says Me is a clsComputer object, so what am I doing wrong?
回答1:
Classic.
pCD.Initialize (Me) 'Error occurs on this line when using F8
Drop the parentheses.
pCD.Initialize Me
Done.
Parentheses around a parameter force it to be evaluated and passed ByVal (regardless of what the procedure's signature says) - and since you probably haven't defined a default property for clsComputer
then the evaluation blows up and the runtime doesn't even get to the Initialize
method.
That said, there's nothing wrong with passing object reference by value. In fact, that's what C# and VB.NET do by default - consider passing any parameter ByVal
.
回答2:
Public Property Set Parent(ByRef Obj As clsComputer)
Set pParent = Obj
End Property
回答3:
I'm not by my PC so just coding blind
Try this for your clsComputer class
Option Explicit
Private pCD As clsCD
''''''''''''''''''''''''''''''
' CD property
''''''''''''''''''''''''''''''
Public Property Get CD() As clsCD
Set CD = pCD
End Property
Public Property Set CD(value As clsCD)
pCD = value
End Property
Sub Class_Initialize()
Set pCD = New clsCD
pCD.Initialize(Me)
End Property
来源:https://stackoverflow.com/questions/38211009/vba-pass-parent-class-to-child-class