Option Explicit
Private mValue As Boolean
'Doesn't matter if this is commented out or not:
'
'Private Sub Class_Initialize()
'
'End Sub
Private Sub Class_Terminate()
'
End Sub
Public Property Get TheValue() As Boolean
TheValue = mValue
End Property
Public Property Let TheValue(ByVal value As Boolean)
mValue = value
End Property
then a stdmodule:
Option Explicit
Public Function CreateStoringClass(ByVal value As Boolean) As StoringClass
Dim result As StoringClass
Set result = New StoringClass
result.TheValue = value
Set CreateStoringClass = result
End Function
' Problem with Office 2013 64-Bit (incl. 2018-09, 15.0.5045.1000):
' This line of code works wrong:
' If Not Factory.CreateStoringClass(VALUE_TO_TEST).TheValue) Then
' Remark:
' The terminator in "StoringClass" causes this behaviour (even if it is empty).
' When it will be removed or commented out, it runs well!
there's a lot more junk and I've tweaked it a bit, but it does demonstrate the weirdness
output on 64-bit Excel is "This is wrong"
if I extract a local variable for the Create result, I get "This is correct"
Sub ShortTest()
Const VALUE_TO_TEST As Boolean = True
Debug.Print "Value to check: " & VALUE_TO_TEST
Dim result As StoringClass
Set result = CreateStoringClass(VALUE_TO_TEST)
If result.TheValue <> VALUE_TO_TEST Then
Debug.Print "This is wrong"
Else
Debug.Print "This is correct"
End If
End Sub
^ works as expected
Sub ShortTest()
Const VALUE_TO_TEST As Boolean = True
Debug.Print "Value to check: " & VALUE_TO_TEST
'Dim result As StoringClass
'Set result = CreateStoringClass(VALUE_TO_TEST)
If CreateStoringClass(VALUE_TO_TEST).TheValue <> VALUE_TO_TEST Then
Debug.Print "This is wrong"
Else
Debug.Print "This is correct"
End If
End Sub
No Class_Terminate handler, no local variable: OK
With Class_Terminate handler, no local variable: terminatingSurprise!
No Class_Terminate handler, with local variable: OK
With Class_Terminate handler, with local variable: OK
terminating
@Comintern so RD needs an inspection for inlined member calls, on user objects (?), I guess. "Object may get terminated before the member call is executed, on 64-bit hosts"
What I want to know is if it's creating the default instance to call the Property Get.
OK, what sort of WTFery is this?
Option Explicit
Private mValue As Boolean
Private Sub Class_Initialize()
Debug.Print "Initializing"
End Sub
Private Sub Class_Terminate()
Debug.Print "Terminating"
End Sub
Public Property Get TheValue() As Boolean
Debug.Print "Accessing TheValue"
TheValue = mValue
End Property
Public Property Let TheValue(ByVal value As Boolean)
Debug.Print "Setting TheValue"
mValue = value
End Property
Output:
Value to check: True
Initializing
Setting TheValue
Accessing TheValue
Terminating
This is wrong
Option Explicit
Private mValue As Boolean
Private instance As Integer
Private Sub Class_Initialize()
Static ctor As Integer
ctor = ctor + 1
instance = ctor
Debug.Print "Initializing " & instance
End Sub
Private Sub Class_Terminate()
Debug.Print "Terminating " & instance
End Sub
Public Property Get TheValue() As Boolean
Debug.Print "Accessing TheValue " & instance
TheValue = mValue
End Property
Public Property Let TheValue(ByVal value As Boolean)
Debug.Print "Setting TheValue " & instance
Only 1 is created.
Value to check: True
Initializing 1
Setting TheValue 1
Accessing TheValue 1
Terminating 1
This is wrong
Value to check: True
Initialized
Setting the value
Getting the value
Getting the result
Terminated
This is correct
(this is on 32-bit host, though)
using this version of test:
Function ShortResult() As Boolean
Debug.Print "Getting the result"
ShortResult = True
End Function
Sub ShortTest1()
Const VALUE_TO_TEST As Boolean = True
Debug.Print "Value to check: " & VALUE_TO_TEST
If CreateStoringClass(VALUE_TO_TEST).TheValue <> ShortResult Then
Debug.Print "This is wrong"
Else
Debug.Print "This is correct"
End If
End Sub