Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Boolean Issues - what am I doing wrong 1

Status
Not open for further replies.

stduc

Programmer
Nov 26, 2002
1,903
GB
Code:
Sub testboolean()
Dim B1 As Boolean, B2 As Boolean, B3 As Boolean
Dim BR As Boolean
    BR = False
    B1 = False
    B2 = True
    B3 = True
    BR = B1 + B2 + B3
    Debug.Print BR
    BR = Not ((B1 + B2 + B3))
    Debug.Print BR
    BR = Not (BR)
    Debug.Print BR
    
End Sub

This is some test code I wrote

The question is - How come the results are...

True
True
False

I would have expected

True
False
True

Why is "Not ((B1 + B2 + B3))" evaluated as true?
 



hi,

BOOLEAN: Then use BOOLEAN OPERATORS!!!

try
Code:
    BR = B1 And B2 And B3



Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
>Why is "Not ((B1 + B2 + B3))" evaluated as true?

Because in VBA 0 is defined as False, and True is defined as Not False (it is simply represented as -1)

B1 + B2 + B3 = 0 + -1 + -1 = -2

And -2 is definte;y Not 0, hence is True
 


Using an ARITHMETIC operator, you got a ARITHMETIC result, as strongm points out.

Look up OPERATORS in VBA Help

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I take your point and it's what I started with but it doesn't do what I want either.

Code:
Sub testboolean()
Dim B1 As Boolean, B2 As Boolean, B3 As Boolean
Dim BR As Boolean
    BR = False
    B1 = False
    B2 = True
    B3 = True
    BR = B1 And B2 And B3
    Debug.Print BR
    BR = Not ((B1 + B2 + B3))
    Debug.Print BR
    BR = Not (BR)
    Debug.Print BR
    
End Sub
Gives results of
False
True
False

If any or all of B1, B2 or B3 are true I want true - only if all are flase do I want false.

gives
 
If [!]any[/!] or all of B1, B2 or B3 are true I want true
BR = B1 [!]Or[/!] B2 [!]Or[/!] B3

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
strongm
I'm sure I was taught that for Boolean a single bit is used and 0 represents false, 1 represents true

Therefore 0+1 = 1 and 1+1=1 - seems I remember wrong though!

PHY - that did the trick - thanks
 
>seems I remember wrong though!

Not exactly :) . The definition you remember - that a Boolean variable may be regarded as a numerical variable with but a single bit which can store only two values - is indeed valid, mostly academically. Implementations in the real world, however, tend to differ.

As long as we only perform boolean operations on our booleans then the implementation does not matter. It's only once you start trying to perform normal mathematical operations on them then that the implementation becomes important (there was a big fuss when VB.NET was originally plannied to evaluate TRUE as 1 rather than VB6's -1 when being converted to a number; Microsoft changed their minds for VB6 compatibility reasons but it actually means VB.NET conversions of booleans differ from the .NET Framework conversions of booleans)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top