Hi,
There are some discrepancies in the OP's examples. The conditions
stated in his first one aren't the same as those in the second.
According to the first example, we have the following conditions to
enable the checkbox:
a) It's checked and dr.PrimarySet > 0
b) It's unchecked and dr.PrimarySet = 0
And we have this condition to disable the CheckBox:
c) It's unchecked and dr.PrimarySet <> 0
Notice that nothing is said about what to do if the checkbox is checked
but dr.PrimarySet <= 0. According to the code, we do nothing, that is,
we leave the checkbox.Enabled property in it's current state. Now, it
may be part of the logic of the application or an omission. We have no
way to tell, since we're not informed about the possible values of
dr.PrimarySet.
Consider now the second example. According to it, we Disable the
Checkbox if:
a) It's not Checked and dr.Primary = 1;
otherwise we enable it.
This states a completelly different logic from the 1st example, and
there's no way to compare both, even though they deal with the same
objects. We can only say that the second example has no relation to the
first, or one of them is a completely mistaken representation.
The OP says he prefers the second example, and issues a statement
similar to:
CheckBox.Enabled = Not( Not CheckBox.Checked _
AndAlso dr.PrimarySet = 1)
To simplify this expression eliminating the double negation, we may
take advantage of the fact that:
Not (A And B) -> Not(A) Or Not(B)
Using our current expression (where A => CheckBox.Checked, B=>
dr.PrimarySet = 1):
Not(Not(A) And B)
-> Not(Not(A)) Or Not(B)
-> A Or Not B
Thus:
CheckBox.Enabled = CheckBox.Checked OrElse dr.PrimarySet <> 1
Now, if we were to simplify the logic of the first example, we should
decide what to do if the checkbox is checked and dr.PrimarySet <= 0.
I'll assume that when the OP stated dr.PrimarySet > 0, he actually
wanted to say dr.PrimarySet <> 0. I will also assume that if the
Checkbox is set but dr.PrimarySet = 0, we must not mess with the
Enabled property. In other words, to set the Enabled property we'll
have to consider it's current value!
These are, thus, our options:
A) CheckBox.Checked And dr.PrimarySet <> 0 -> Enabled = True
B) CheckBox.Checked And dr.PrimarySet = 0 -> Enabled = Enabled
C) CheckBox.Unchecked and dr.PrimarySet = 0 -> Enabled = True
D) CheckBox.Unchecked And dr.PrimarySet <> 0 -> Enabled = False
If we were to state this logic in a single expression, it could be:
Dim A As Boolean = CheckBox.Checked
Dim B As Boolean = dr.PrimarySet <> 0
Dim C As Boolean = CheckBox.Enabled
CheckBox.Enabled = Not(A OrElse B) _
OrElse (A AndAlso (B OrElse C))
Arguably, it's much more clear to use an If:
If CheckBox.Checked Then
CheckBox.Enabled = (dr.PrimarySet <> 0) OrElse CheckBox.Enabled
Else
CheckBox.Enabled = dr.PrimarySet = 0
End If
In any way, let's hope the VB designers introduce a ternary operator in
the language for the next version:
CheckBox.Enabled = CheckBox.Checked? _
(dr.PrimarySet <> 0) OrElse CheckBox.Enabled | dr.PrimarySet = 0
=))))
Have fun,
Branco