2016-07-05 3 views
0

내가 정확히 내가 원하는 것 같다 SO에 큰 게시물을 발견 : Is it possible to access a parent property from a child that is in a collection? 그러나 내 적응이 나에게주고있다 Object doesn't support this property or method.VBA 패스 부모 클래스가

지금 매트의 머그잔 덕분에 작동

내 코드와 Tomalak :

부모 클래스 - 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

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 

, 그것은 객체입니다 (예를 들면, If IsObject(Me) Then Stop 사실 평가) 및 IntelliSense를 모두 보여줍니다 내가 입력했을 때 clsComputer의 속성 및 메서드 Me. 지역 창에 Me가 clsComputer 개체로 표시됩니다. 확인을 위해 알아두면 모든 것이 clsComputer 객체라는 것을 알기 때문에 내가 뭘 잘못하고있는 걸까요?

답변

2

클래식이 시도 블라인드

코딩 내 PC가 아니에요.

pCD.Initialize (Me) 'Error occurs on this line when using F8 

버리기 괄호

.

pCD.Initialize Me 

완료. 이 될 수있는 매개 변수 힘 주위

괄호는을 평가 (관계없이 절차의 서명이 말씀의) ByVal의을 통과 - 그리고 당신은 아마 clsComputer의 기본 속성을 정의하지 않았기 때문에 다음 평가는 불면과 런타임도 Initialize 메소드에 도달하지 않습니다.

즉, 값으로 객체 참조를 전달하는 데는 아무 문제가 없습니다. 사실 C#과 VB.NET이 기본적으로 수행하는 것입니다. 매개 변수 ByVal을 전달하는 것을 고려하십시오.

+1

아하! 이것과 @tomalak 사이에, 그것을 수정합니다!나는 나의 포스트를 새롭게 할 것이다. – Tim

+0

굉장합니다. 마침내 저는 팸알을 원한다는 마지막 비트를 알고 있습니다. 감사. – Tomalak

2
Public Property Set Parent(ByRef Obj As clsComputer) 
    Set pParent = Obj 
End Property 
+0

주사위가 없습니다. 여전히 오류가 발생합니다. ByRef는 개체의 기본값이므로 Let to Set에서 변경하면 문제가 해결되지 않습니다. – Tim

+1

그러나 AFAIK라는 객체 참조도 할 수 없습니다. 틀 렸으면 고쳐줘. – Tomalak

+0

잘못이 아닙니다. 개체 참조를 참조로 전달하는 대신 값으로 전달합니다. @Tim 'Property Let'는 값 유형을 나타냅니다. 객체에'Property Set'을 사용하십시오. –

0

은 그래서 당신의 clsComputer 클래스

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 
관련 문제