VBA将父级传递到子级 [英] VBA pass parent class to child class
问题描述
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.
我的代码现在可以工作了,这要感谢Mat's Mug和Tomalak:
My code which now works thanks to Mat's Mug and Tomalak:
父类-clsComputer
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
子类-clsCD
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
代码模块-Module1
Code Module - Module1
Sub test()
Dim oPC As clsComputer
Set oPC = New clsComputer
Debug.Print "CD Status: " & oPC.CD.Status
End Sub
如果我测试我,它是一个对象(例如,如果IsObject(Me)然后Stop
计算为true),并且当我键入 Me时,Intellisense将显示clsComputer中的所有属性和方法.
本地"窗口将我显示为clsComputer对象.我知道要检查的所有内容都说我是一个clsComputer对象,所以我在做什么错了?
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?
推荐答案
经典.
pCD.Initialize (Me) 'Error occurs on this line when using F8
删除括号.
pCD.Initialize Me
完成.
围绕参数进行强制解析,强制对其进行评估并传递ByVal (无论过程签名怎么说)-而且由于您可能尚未定义默认属性对于 clsComputer
,则评估会崩溃,并且运行时甚至无法使用 Initialize
方法.
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.
也就是说,按值传递对象引用没有错.实际上,这就是C#和VB.NET的默认设置-考虑传递 any 参数 ByVal
.
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
.
这篇关于VBA将父级传递到子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!