Results 1 to 13 of 13

Thread: When True isnt True

  1. #1

    Thread Starter
    Lively Member vbLewis's Avatar
    Join Date
    Feb 2009
    Location
    USA
    Posts
    126

    When True isnt True

    Can someone smarter than me help me wrap my head around what is happening here? the expression (bBefore = True) is returning false even though the value of bBefore is true.

    Name:  Image2.jpg
Views: 268
Size:  33.3 KB

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: When True isnt True

    bBefore seems to be defined as Boolean so no idea what is happening.
    What happens if you replace the line with:
    Code:
    If (lng_hWnd = Me.hWnd) And bBeFore Then

  3. #3

  4. #4
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,997

    Re: When True isnt True

    The Boolean type can hold other than 0 an -1 internally, specially when the result comes from an API call.

    In that case it can report like "True" in the tooltip in the IDE but internally it has for example 1, then the comparison (bBefore = True) reports as False, because 1 = -1 is False.

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: When True isnt True

    Yes, Eduardo has it.

    As a quick fix, just DON'T compare bBefore to True. Just test it "stand-alone":

    Code:
    
    If lng_hWnd = Me.hWnd And bBefore Then
    
    Doing it like the above, any non-zero value in bBefore will return True. I've talked about this before in detail. Basically, in VB6, a Boolean is just a special case of an Integer. And, in VB6, True = &hFFFF. However, you can put any value into a Boolean that you can put into an Integer.

    vbLewis, we need to see the code where bBefore is being assigned. I'm guessing it's some kind of return from a DLL, probably written with C conventions. In C (and related languages), True = &h0001 (or, in C nomenclature, True = 0x0001). As we see, &h0001 <> &hFFFF, so, a comparison of the two will return False.

    You could "fix" bBefore by doing something like the following (after it's initially assigned):

    Code:
    
    bBefore = bBefore <> False
    
    I hope that helps.

    EDIT: Let me talk a bit more about what the "If lng_hWnd = Me.hWnd And bBefore Then" statement is doing. It's a bit weird because we don't have "logical" AND and OR operators in VB6. So, what it's is:
    1. Evaluating "lng_hWnd = Me.hWnd". For the sake of argument, let's assume that's True (which will return &hFFFF).
    2. And then, it ANDs (bitwise) that value to whatever is in bBefore. Again, for the sake of argument, let's assume a &h0001 is in there. Therefore, we're doing: &hFFFF AND &h0001, which will return &h0001.
    3. Since &h0001 is non-zero, the entire IF expression will then return True, and execute the THEN portion.

    Initially, the way you had it, "bBefore = True" would return False, because &hFFFF doesn't equal &h0001.

    -----

    EDIT2: Above I said, "you can put any value into a Boolean that you can put into an Integer." Technically, that's true. However, VB6 makes it a bit tricky to do that. For instance, the following code ...

    Code:
    
    Dim b as Boolean
    b = 5
    
    ... will put a True (&hFFFF) into the b Boolean. VB6 "sees" that it's a Boolean, and "converts" any non-zero value to True. Therefore, about the only way to get other values into a VB6 Boolean variable is to do some memory copy (i.e., GetMem2), or the return from some API call. There are a few other ways (such as a UDT and a LSet statement), but those are the most obvious.
    Last edited by Elroy; Jun 22nd, 2020 at 11:21 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  6. #6

    Thread Starter
    Lively Member vbLewis's Avatar
    Join Date
    Feb 2009
    Location
    USA
    Posts
    126

    Re: When True isnt True

    You guys are on to something...

    doing this
    Code:
    Debug.Print CLng(bBefore)
    gave me
    Code:
    -24576
    I changed the logic to
    Code:
    (bBefore <> False)
    and now we are humming along great!

    i was always under the impression that a Boolean was forced to be -1 or 0 by VB but i learned something new today!
    Now my slider control BG is vbWhite to match the BG of the form it is on and Im happy as can be!

    Thank you guys for all the replies and the help!

  7. #7
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: When True isnt True

    Testing got bBefore = True not only yeilds wrong results sometimes but it generates extra bytecode as well.

    That's why a code like If (pObject Is Nothing) = True Then smells a bit. This not only generates extra bytecode but it also reduces overall readability IMO.

    Btw, the proposed If lng_hWnd = Me.hWnd And bBefore Then evaluation when bBefore = 5 can be entertaining as well.

    When lng_hWnd = Me.hWnd is evaluated to &HFFFF this gets bitwise ANDed with 5 which yields 5 which is <> 0 so success so enter cons part.

    With quirky booleans participating in a boolean expersion this can evaluate to something quirky as well.

    cheers,
    </wqw>

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: When True isnt True

    Quote Originally Posted by wqweto View Post
    With quirky booleans participating in a boolean expersion this can evaluate to something quirky as well.
    Yeah, it'd be nice if we had the equivalent of && and || operators, but we don't, so we make due.

    Also, those operators wouldn't have fixed vbLewis's problem. It's just important to remember that a VB6 Boolean is just a special case of a VB6 Integer, which 99% of the time has either &h0000 (False) or &hFFFF (True) in it. But it's that 1% that sometimes throws us.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  9. #9
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: When True isnt True

    Boolean operators can be emulated like this

    A && B <=> ((A And B) <> 0)
    A || B <=> ((A Or B) <> 0)

    So quirkyness can be easily removed by <> 0 like this

    lng_hWnd = Me.hWnd And bBefore = True => lng_hWnd = Me.hWnd And CInt(bBefore) <> 0

    . . . and you go to "normal" booleans land again.

    cheers,
    </wqw>

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: When True isnt True

    Quote Originally Posted by wqweto View Post
    Boolean operators can be emulated like this

    A && B <=> ((A And B) <> 0)
    A || B <=> ((A Or B) <> 0)
    Hi wqweto,

    That's not quite true though:

    In C:
    Code:
    short A = 1;
    short B = 2;
    short C = A && B; // <---- C will equal 1 (True).
    In VB6:
    Code:
    Dim A as Integer: A = 1
    Dim B as Integer: B = 2
    Dim C as Integer: C = (A And B) <> 0 ' <---- C will equal 0 (False).
    The same kind of example could be worked up for the OR as well.

    EDIT: Also, to tamp down confusion, I tend to call the "logical" versus "bitwise". I know that doesn't exactly coincide with the VB6 MSDN, but it still makes more sense to me, and does agree with C, C#, and C++ help systems.

    EDIT2: Actually, I think your idea will work for the OR. It just won't work for the AND. It's got to be: C = A <> 0 And B <> 0
    Last edited by Elroy; Jun 22nd, 2020 at 12:59 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  11. #11
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: When True isnt True

    Quote Originally Posted by Elroy View Post
    It just won't work for the AND. It's got to be: C = A <> 0 And B <> 0
    Yes, this was not very precise on my part.

    And so bitwise Not A can become A = 0 for a logical Not and then some DeMorgan A && B <=> Not (Not A || Not B) <=> (A = 0 Or B = 0) = 0 but this is too convoluted so A <> 0 And B <> 0 is probably easiest.

    cheers,
    </wqw>

  12. #12
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: When True isnt True

    @vbLewis:

    I keep thinking about this, and the best way to make it most simple. Here's a pretty simple way to think about it...

    There is only one value for False (regardless of the language), and it's ZERO.

    However, there are many values for True. In fact, anything non-ZERO is considered True ... and there's the rub.

    So, in a VB6 Boolean, when we print any of these values, they will print "True". However (and this is a BIG however), when we start comparing those different values of True to each other, they will NOT be equal. So, a comparison of True (in one form) equal to True (in another form) will be FALSE (which is precisely what happened to you).

    Best Regards,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  13. #13
    Addicted Member
    Join Date
    Feb 2015
    Posts
    154

    Re: When True isnt True

    Great discussion . I learned something new today too!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width