0

Unfortunately, sometimes when the if statements are more complex, behavior is quite strange, here is a copy of the line which doesnt work as expected when used like this

If someFunction(arg1, arg2, CreatedFromClass(arg3, arg4, arg5)) Then GoTo err1

someFunction(...) itself has return type long, but in this case is absolutely always evaluated as positive, even though someFunction before return contains 0 as a return value.

so, it can be wrapped as any of the following and result will be always true

If someFunction() = True Then
If someFunction() = False Then
If someFunction() = 0 Then
If someFunction() = 1 Then
If someFunction() <> 0 Then
If someFunction() <> 1 Then
If Not someFunction() = 0 Then
If Not someFunction() = 1 Then
If (someFunction() = 0) = False Then
If (someFunction() = 0) = True Then
If CBool(someFunction() = 0) = True Then
If CBool(someFunction() = 1) = True Then

but when is split into segments, and manually assigned to the variable, then works as expected

Dim rVal As Long
rVal = someFunction(arg1, arg2, CreatedFromClass(arg3, arg4, arg5))
If rVal Then GoTo err1

question is of course why the behavior is not the same? did someone experienced similar?

Additionally, after couple hours, i been able to isolate problematic piece of code!

Class: cls.cls

Option Explicit

Private mCol As Collection

Private Sub Class_Initialize()
    Set mCol = New Collection
End Sub

Private Sub Class_Terminate()
    Set mCol = Nothing
End Sub

Public Sub Add(iItem)
    mCol.Add iItem
End Sub

Public Function DoCopy() As cls
    Set DoCopy = New cls

    Dim i As Long
    For i = 1 To mCol.Count
        DoCopy.Add mCol(i)
    Next
End Function

and testing module

Option Explicit

Public Function CreateCls(ParamArray params()) As cls
    Set CreateCls = New cls
    If UBound(params) < 0 Then Exit Function

    Dim i As Long
    For i = LBound(params) To UBound(params)
        CreateCls.Add params(i)
    Next
End Function

Private Function doTest(iCls As cls) As Long
    doTest = 0
End Function

Private Sub CATStart()
    Dim x As cls
    Set x = CreateCls("param1", "param2", "param3", "param4")

    If doTest(x.DoCopy) = 0 Then Debug.Print "long_0"
    If doTest(x.DoCopy) = 1 Then Debug.Print "long_1"
    If CBool(doTest(x.DoCopy)) = True Then Debug.Print "bool_True"
    If CBool(doTest(x.DoCopy)) = False Then Debug.Print "bool_False"

    Set x = Nothing
End Sub

And of course all of them will be evaluated as True valid and result printed!

tsolina
  • 141
  • 15
  • 1
    The behaviour is well defined. I'd say that in your case, someFunction is throwing an error and that you have an OnError Resume Next before previously. Without a reproducible example, this is the only thing that makes sense to the behaviour you are describing. – Florent B. Mar 23 '16 at 19:25
  • Following up on Florent B, avoid `On Error Resume Next` at all costs. If you must use it, put it inside a very specialized function complete with profuse comments on why it's really really needed. Otherwise adopt the attitude that there's never a good reason for `On Error Resume Next`. It leads to strange things like this. – rskar Mar 23 '16 at 19:33
  • I completely agree, and i hate and don't use On Error Resume Next unless impossible to avoid ( 3rd party stuff ) and even then is very well structured code around it.. even this goto err1 is not error handler, its just handler for different return values, pure longs ( c style returns ) – tsolina Mar 24 '16 at 10:18
  • I've experienced this behavior when trying to evaluate booleans in IF statements. I've noticed this even more when trying simply `If someFunction() Then...` will yield false positives. In these cases, your solution at the bottom is typically what I've done and works...probably some kind of bug in VBA...A lot of hours were wasted trying to understand how FALSE=TRUE. – GisMofx Mar 24 '16 at 14:27

1 Answers1

0

And yes, i found an answer on my question, in Catia, i have VBA version 7.1.1033, which doesn't work as expected, while the the same piece of code works in Excel, which uses newer VBA version, 7.1.1049...

So answer is, VBA has bug in that release..

tsolina
  • 141
  • 15