I’ve been using this for long time. Now I want to know how it works. I have an example:
If e.State And DrawItemState.Selected Then
'if e.state includes DrawItemState.Selected do something
End If
E.state has options like 1 2 4 8 16 32 64 128 and so on… Also e.State can include more then one of them. For example 4 + 32 which is 36 includes two states. I can check them with “and” operator. I am totaly aware of usage, yet I have no idea how it works. Also I am wondering is it more efficent then checking each condition with a “Select Case” block.
EDIT: I now understood the logic. How about efficency?
Your code actually conflates the logical and bitwise AND operators: there is an implicit cast, which throws a compiler error if you turn Option Strict On. It’s more “correct” to only use the bitwise operator, as follows:
Your question is a very good one. Visual Basic’s AND operator is a bit confusing in this regard, and I believe many programmers would have the same confusion. I certainly did, back in the day 🙂
As for efficiency compared to SELECT CASE, it depends whether you want to handle each flag on its own, or handle a pattern of flags.
It helps if you think of e.State as a panel of light switches that are each either on or off. SELECT CASE can be used to handle patterns of switches, but it can’t be used to handle individual switches.
for example
will only trigger if e.State is ONLY .Selected. If e.State was .Selected plus .Focus, like this:
then that CASE statement WOULD NOT trigger.