VBA 将 parent class 传递给 child class
VBA pass parent class to child class
我在 SO 上找到了一个很棒的 post,这似乎正是我想要的:Is it possible to access a parent property from a child that is in a collection? 但是我对它的改编给了我 Object doesn't support this property or method.
感谢 Mat's Mug 和 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
代码模块 - Module1
Sub test()
Dim oPC As clsComputer
Set oPC = New clsComputer
Debug.Print "CD Status: " & oPC.CD.Status
End Sub
如果我测试 Me,它是一个 object(例如,If IsObject(Me) Then Stop
评估为真),并且当我键入 Me.
The Locals 时,Intellisense 会显示 clsComputer 中的所有属性和方法windows 将我显示为 clsComputer object。我知道要检查的所有内容都说我是 clsComputer object,那么我做错了什么?
Public Property Set Parent(ByRef Obj As clsComputer)
Set pParent = Obj
End Property
我不在电脑旁,所以只是盲目编码
为您的 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
经典。
pCD.Initialize (Me) 'Error occurs on this line when using F8
去掉括号。
pCD.Initialize Me
完成。
参数周围的圆括号 强制它被求值 并通过 ByVal(不管过程的签名说什么)——并且因为你可能还没有为 clsComputer
定义一个默认的 属性 然后评估爆炸并且运行时甚至没有到达 Initialize
方法。
也就是说,按值传递对象引用没有错。事实上,这就是 C# 和 VB.NET 默认执行的操作 - 考虑传递 any 参数 ByVal
.
我在 SO 上找到了一个很棒的 post,这似乎正是我想要的:Is it possible to access a parent property from a child that is in a collection? 但是我对它的改编给了我 Object doesn't support this property or method.
感谢 Mat's Mug 和 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
代码模块 - Module1
Sub test()
Dim oPC As clsComputer
Set oPC = New clsComputer
Debug.Print "CD Status: " & oPC.CD.Status
End Sub
如果我测试 Me,它是一个 object(例如,If IsObject(Me) Then Stop
评估为真),并且当我键入 Me.
The Locals 时,Intellisense 会显示 clsComputer 中的所有属性和方法windows 将我显示为 clsComputer object。我知道要检查的所有内容都说我是 clsComputer object,那么我做错了什么?
Public Property Set Parent(ByRef Obj As clsComputer)
Set pParent = Obj
End Property
我不在电脑旁,所以只是盲目编码
为您的 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
经典。
pCD.Initialize (Me) 'Error occurs on this line when using F8
去掉括号。
pCD.Initialize Me
完成。
参数周围的圆括号 强制它被求值 并通过 ByVal(不管过程的签名说什么)——并且因为你可能还没有为 clsComputer
定义一个默认的 属性 然后评估爆炸并且运行时甚至没有到达 Initialize
方法。
也就是说,按值传递对象引用没有错。事实上,这就是 C# 和 VB.NET 默认执行的操作 - 考虑传递 any 参数 ByVal
.