Re: Differences between VB6 and VB.Net causing problem



Hi Schmidt,

I didn't test that code but I presume it forces a value into a boolean other than 0 or -1. This is the exact concept I was referring to. I showed Larry the same using CopyMemory, but I don't think he got it.

There's a couple of important things to understand:
(1) a boolean is not guaranteed to have any internal value. If it did it would make it tri-state. It's two states are 0 and non 0
(2) Never compare a Boolean to True. That's a bitwise operation that depends on the internal state which is not guaranteed
(3) The operators And, equality =, Or, XOr and Not are all bitwise. They are not boolean operators. The only boolean operator in VB6 is the CBool one.
(4) This one's for Larry. Bitwise operators require the types to be bitwise. In VB6 only byte, Integer and Long are bitwise numerics. You can't do bitwise operations directly on a Decimal type: a conversion, implicit or otherwise, is required to one of the bitwise types first.
(5) Also for Larry: True is 16 bit. Boolean is 16 bit.




"Schmidt" <sss@xxxxxxxxx> wrote in message news:%23u6am3wMJHA.5840@xxxxxxxxxxxxxxxxxxxxxxx

"Larry Serflaten" <serflaten@xxxxxxxxxxxxxx> schrieb im Newsbeitrag
news:OyJOFBpMJHA.3588@xxxxxxxxxxxxxxxxxxxxxxx

Suffice it to say, that in types other than Byte and String, -1
is the value stored when representing True.

To add some fun into the discussion... the above is 'True',
... and just to formulate it a bit "stronger":
Dont force a boolean compare or Bit-Op, if you cannot
be sure that the Variable really contains 0 or -1.

The following snippet shows, that you cannot even rely
on the content of VBs Boolean-Variables, to be *always*
0 or -1.

It came up some years ago in the german groups - I only
readjusted the example a bit, to be more comprehensible,
covering a (more or less) "realistic" scenario...

Option Explicit

Private Sub Form_Load()

If AppInitializeNeeded Then 'we don't have files in the App.Path
'so we should initialize
Debug.Print "Really... no Files there?"

Else 'proceed normally with your App


End If

End Sub

Function AppInitializeNeeded() As Boolean
Dim boolFilesFound As Boolean 'we rely on the cast on this variable

FilesExistInPath App.Path, boolFilesFound

AppInitializeNeeded = Not boolFilesFound

'just to shade some light on VBs misbehaviour...
'the result of that statement is really "funny" ;-)
Debug.Print CInt(boolFilesFound);
End Function

Sub FilesExistInPath(Path As String, VReturn As Variant)
Dim FileCount As Long

'Determine the FileCount in a DirLoop for example -
'here we only simulate the result, stored in a Long
FileCount = 3

'now we pass the result back in the ByRef-Param
VReturn = FileCount
End Sub

Olaf



.



Relevant Pages

  • Re: Why is "False" = "-1"?
    ... > matter how many bits. ... Logical operators require that all inputs are booleans, ... generate boolean states as a result. ... But unlike C, classic VB has no logical operators, only bitwise ...
    (microsoft.public.vb.general.discussion)
  • Re: Overloaded logic operators
    ... x and x.foo# if x is not nil, ... In languages such as Ruby, Lua, Io, Python and JavaScript, does the ... Let the default boolean type be a type with more than two values, ... includes sequential logical operators that are not bitwise). ...
    (comp.compilers)
  • Re: True = -1 ?
    ... logical refers to a boolean state. ... call the two types of operations bitwise and Boolean. ...
    (microsoft.public.vb.general.discussion)
  • Re: Boolean data type?
    ... What type is commonly used in C for playing around with boolean ... It might even make sense to store a boolean value in each bit ... (but you'll have to write your own bitwise masking and shifting code ... Assuming C99's _Bool is not available, ...
    (comp.lang.c)
  • Re: Why is "False" = "-1"?
    ... Logical operators require that all inputs are booleans, ... generate boolean states as a result. ... But unlike C, classic VB has no logical operators, only bitwise ... operators and boolean data types. ...
    (microsoft.public.vb.general.discussion)