VB6 WTF



  • Dim doneLoading As Boolean

    Private Sub Form_Load()

        comObject.StartProcessing 1
        doneLoading = False
        While Not doneLoading
            DoEvents
        Wend

    End Sub 

    Private comObject_Process(ByVal bDone As Boolean)
        doneLoading = bDone
    End Sub

    Surprisingly, even when the event fires with bDone set to true, this remains in an infinite loop. Breaking into the debugger in the While loop, and examining doneLoading shows that it's value is indeed True.

    WTF

    Just another reason why I think VB6 is useless for anything important.



  • VB6 can do many things important...

     

    The problem here's called not knowing what you're doing.....  RoFL......

     

    On a single thread, you're setting doneLoading to true and then IMMEDIATELY setting it back to false before your loop..  Of COURSE your loop's going to keep running infinately..

     

     



  • I've seen this kind of thing in VB6 before, although the fault is partly in the COM object not correctly following the specs.

    You see, the Boolean data type in VB6 is 2 bytes in length. (Yes, 2 bytes. Really.)
    False is represented internally as 0 (&H0000), and True as -1 (&HFFFF) (For those not privledged enough to work with VB that much, the "&H" is equivilent to C's "0x"). You never see these representations in VB, but that's how it works internally.

    The Not operator in VB is a bit-wise not. This works just fine on the Boolean values above.

    However, a COM component might not follow the rules for the Boolean data type exactly, and could put a value of 1 (&H0001) in a Boolean value to mean True. When used in a conditional, VB also treats this value as true.

    But, when you use the Not operator on it, you get -2 (&HFFFE). Which since it's not 0, VB also treats as true.

    So when looking in the debugger, the value of the variable is True, and value of the Not of the variable is also true.

    Which means that I've actually written "Variable = (Variable = True)" in my code, in order to fix this crazy problem. (And yes, I had a big comment above it explaining exactly why that line of code was there.)



  • @LoCInuyasha said:

    VB6 can do many things important...

     

    The problem here's called not knowing what you're doing.....  RoFL......

     

    On a single thread, you're setting doneLoading to true and then IMMEDIATELY setting it back to false before your loop..  Of COURSE your loop's going to keep running infinately..

     

     

     You're misreading the code. It doesn't get set back to false. It sounds like you are one of those who doesn't even really know what VB is doing... I initialize the value to false. Then, I start the loop. In the loop I call DoEvents, which processes any messages in the queue. One of those message is sent from the com object at some point. The DoEvevnts causes this message to be processed and the event to be called, where the value of doneLoading is set to true. Then, the loop continues.

    pcooper had the problem identified correctly. Replacing the event code with below solves this problem.


        If bDone Then
            doneLoading = True
        End If
     

    Basically, this issue arises from VB not handling boolean values like nearly every other programming language. It's also the fault of the COMObject though. 



  • @LoCInuyasha said:

    VB6 can do many things important...

     

    The problem here's called not knowing what you're doing.....  RoFL......

     

    On a single thread, you're setting doneLoading to true and then IMMEDIATELY setting it back to false before your loop..  Of COURSE your loop's going to keep running infinately..

     

     

    Ah, I see now what you thought was happening. Yeah, the COM object is asynchronous... that event doesn't get called for quite some time... Although I should've put the initialization before the call to start processing. (not like any of this is the actual code anyway... actual code is "correct")



  • @pcooper said:

    I've seen this kind of thing in VB6 before, although the fault is partly in the COM object not correctly following the specs.

    You see, the Boolean data type in VB6 is 2 bytes in length. (Yes, 2 bytes. Really.)
    False is represented internally as 0 (&H0000), and True as -1 (&HFFFF) (For those not privledged enough to work with VB that much, the "&H" is equivilent to C's "0x"). You never see these representations in VB, but that's how it works internally.

    The Not operator in VB is a bit-wise not. This works just fine on the Boolean values above.

    However, a COM component might not follow the rules for the Boolean data type exactly, and could put a value of 1 (&H0001) in a Boolean value to mean True. When used in a conditional, VB also treats this value as true.

    But, when you use the Not operator on it, you get -2 (&HFFFE). Which since it's not 0, VB also treats as true.

    So when looking in the debugger, the value of the variable is True, and value of the Not of the variable is also true.

    Which means that I've actually written "Variable = (Variable = True)" in my code, in order to fix this crazy problem. (And yes, I had a big comment above it explaining exactly why that line of code was there.)

     

    Noted and filed in the appropriate mental directory.


    Thank you. 


Log in to reply