VBA True or Not False

Programming and macros
User avatar
SolidKeke
Posts: 36
Joined: Wed Apr 07, 2021 5:34 am
Answers: 0
Location: Finland
x 7
x 19

VBA True or Not False

Unread post by SolidKeke »

Hey,

I have tested these lines and I think it's a bug. It happens only when testing suppression state of a feature. Or Can anyone explain what is happening below:

'This is the correct behaviour
Debug.Print True = True
Debug.Print Not True = False

'This is wrong behaviour
Debug.Print swFeat.IsSuppressed = True
Debug.Print Not swFeat.IsSuppressed = True

'It makes this kind of use impossible because condition is always True
If Not swFeat.Issuppressed Then
'code here
End If


'Anyhow, this was the solution to the problem. I don't understand how this is different to code above.
If swFeat.IsSuppressed = False Then
'code here
End If
by AlexB » Fri Sep 09, 2022 8:29 am
A lot of times, the NOT operator doesn't really do what I'm looking for. In those cases, I'll use the <> operator.

For example:

Code: Select all

If myFeature.IsSuppressed <> True Then
   'Do something when suppression state is false
End If
Go to full post
Best Regards,
SolidKeke
User avatar
AlexLachance
Posts: 2036
Joined: Thu Mar 11, 2021 8:14 am
Answers: 17
Location: Quebec
x 2191
x 1892

Re: VBA True or Not False

Unread post by AlexLachance »

I think this might answer your question:
There are precedence rules that govern the order in which operations are performed, when an expression contains more than one operator. In the case of this expression …

False and not True or True

not has highest precedence, so its operation is performed first.

and is performed next.

or has the lowest precedence, so it is performed last.

Here, parentheses have been added to clarify the order of the operations:

(False and (not True)) or True

So, stepwise, the expression is evaluated like this:

1: (False and (False)) or True
2: (False) or True
3: True
Here's an example we use in here.

Sheet are named "Feuille#" by default on a French SolidWorks.
A sheet must be named "Feuille#" for it to be concidered a "black and white drawing"
If the sheet contains DXF in it's name, our print task will not print it. It will be later 'printed' by our task that creates our DXF.
If a sheet contains "COLOR" in it's name, our print task will know this sheet requires to be printed in color.
So any sheet used to generate a DXF will not create a PDF, any sheet that needs colors will have their sheet name accordingly.
By default every drawing prints in black and white because of the default name of the sheet. If they require colors, then the name of their sheet is adjusted accordingly. We also have a sort of macro that creates our drawing sheet for DXF purposes.
User avatar
AlexB
Posts: 452
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 24
x 243
x 401

Re: VBA True or Not False

Unread post by AlexB »

A lot of times, the NOT operator doesn't really do what I'm looking for. In those cases, I'll use the <> operator.

For example:

Code: Select all

If myFeature.IsSuppressed <> True Then
   'Do something when suppression state is false
End If
User avatar
Frederick_Law
Posts: 1847
Joined: Mon Mar 08, 2021 1:09 pm
Answers: 8
Location: Toronto
x 1551
x 1404

Re: VBA True or Not False

Unread post by Frederick_Law »

It always safer to compare two variable.
If the code can be optimize, the complier will do so.

I think the reason is object and method.
myFeature.IsSuppressed return a boolean, it itself is not a boolean.
So (Not myFeature.IsSuppressed) got ignored. It should throw an error.
User avatar
josh
Posts: 263
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 11
x 19
x 456

Re: VBA True or Not False

Unread post by josh »

Please see this article in SW help. Short summary:
Do not use logical operators with returns of these functions. Always compare returned value to False.

https://help.solidworks.com/2019/englis ... _types.htm
Post Reply