For even more fun let's add a dog house into the mix.
Class "clsDogHouse" (Use ByRef or ByVal, doesn't matter because you get the same result)
Code:
Option Compare Database
Option Explicit
Implements clsIDogHouse
Private m_DogInHouse As clsIDog
Private Property Set clsIDogHouse_DogInHouse(ByRef value As clsIDog)
Set m_DogInHouse = value
End Property
Private Property Get clsIDogHouse_DogInHouse() As clsIDog
Set clsIDogHouse_DogInHouse = m_DogInHouse
End Property
Public Sub clsIDogHouse_PutDogInHouse(ByRef inpDog As clsIDog)
Set clsIDogHouse_DogInHouse = inpDog
End Sub
Public Function clsIDogHouse_IsOccupied() As Boolean
clsIDogHouse_IsOccupied = False
If Not (clsIDogHouse_DogInHouse Is Nothing) Then clsIDogHouse_IsOccupied = True
End Function
Public Sub clsIDogHouse_MagicAct()
Set clsIDogHouse_DogInHouse = Nothing
End Sub
Class "clsIDogHouse"
Code:
Option Compare Database
Option Explicit
Property Set DogInHouse(ByRef value As clsIDog): End Property
Property Get DogInHouse() As clsIDog: End Property
Sub PutDogInHouse(ByRef inpDog As clsIDog): End Function
Function IsOccupied() As Boolean: End Function
Sub MagicAct(): End Sub
Add this to Module1
Code:
Public Sub DogHouseTest()
Dim myDog As clsIDog
Set myDog = New clsDog
Dim hisHouse As clsIDogHouse
Set hisHouse = New clsDogHouse
Call ChangeDog1(myDog)
Call hisHouse.PutDogInHouse(myDog)
Debug.Print "Your dog, " & myDog.DogName & _
IIf(hisHouse.IsOccupied, ", is in the house", "is not in the house")
Call hisHouse.MagicAct
If (myDog Is Nothing) Then Debug.Print "Your dog performed a magic trick"
If (myDog Is Nothing) Then Call CloseDogByVal(myDog)
If (myDog Is Nothing) Then Call CloseDogByRef(myDog)
If (myDog Is Nothing) Then Debug.Print "Your dog ran away..."
Set hisHouse = Nothing
If (hisHouse Is Nothing) Then Debug.Print "His house is gone now..."
If (myDog Is Nothing) Then Call CloseDogByVal(myDog)
If (myDog Is Nothing) Then Call CloseDogByRef(myDog)
If (myDog Is Nothing) Then Debug.Print "Your dog ran away..."
End Sub
The output of running DogHouseTest() gives this:
Your dog, Fido, is in the house
His house is gone now...
I am SO LOST! The dog is not gone but his house is, even though we set the dog to nothing in the dog house class and four times in DogHouseTest()
It just doesn't make sense to me.