How to check = in enumeration

  • Thread starter Thread starter null
  • Start date Start date
N

null

I am using the NotifyFilters enumeration type. In 'integer' the values
are ored together, and I can use AND to check for equality like so

If bNotifyFilter And NotifyFilters.Attributes Then
However, when option strict is on this does not work, any ideas?
 
I am using the NotifyFilters enumeration type. In 'integer' the values
are ored together, and I can use AND to check for equality like so

If bNotifyFilter And NotifyFilters.Attributes Then
However, when option strict is on this does  not work, any ideas?

The second branch of the If isn't a comparison, you just have an enum
value sitting there. I'd help you rewrite it, but you haven't given
enough code for me to really see what is going on. Could you post more
code, including the declarations and initialization of the variable
"bNotifyfilter", so I could take a look?

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Ok, here is the definition

Dim enum_notifyFilter As System.IO.NotifyFilters ' = 383

If enum_notifyFilter And NotifyFilters.Attributes Then
....
End If
 
Ok, here is the definition

Dim enum_notifyFilter As System.IO.NotifyFilters ' = 383

If enum_notifyFilter And NotifyFilters.Attributes Then
...
End If

You would want to do plain old = check here, the And will only apply
if you want to see if there are multiple matches (like if you were
using the enum as a bit flag).

////////////
If enum_notifyFilter = NotifyFilters.Attributes Then

End If
////////////

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
In
rowe_newsgroups said:
You would want to do plain old = check here, the And will only apply
if you want to see if there are multiple matches (like if you were
using the enum as a bit flag).

////////////
If enum_notifyFilter = NotifyFilters.Attributes Then

Actually you should check that enum_notifyFilter contains the value not is
exactly equal to the value, e.g:

If enum_notifyFilter And NotifyFilters.Attributes = NotifyFilters.Attributes
Then
 
In



Actually you should check that enum_notifyFilter contains the value not is
exactly equal to the value, e.g:

If enum_notifyFilter And NotifyFilters.Attributes = NotifyFilters.Attributes
Then

Frak.

Thanks for the correction, apparently I'm a bit out of it this
morning!

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
You would want to do plain old = check here, the And will only apply
Actually you should check that enum_notifyFilter contains the value not is
exactly equal to the value, e.g:

If enum_notifyFilter And NotifyFilters.Attributes = NotifyFilters.Attributes
Then

What I need is for this to work, try turning on option strict, and you will
see
it does not allow to convert boolean to enumeration. I cannot simply write

If enum_notifyFilter = NotifyFilters.Attributes then
....
end if

simply because enum_notifyFilter can contain multiple values, in which case
= will fail. What I need is "contained in".
 
null said:
What I need is for this to work, try turning on option strict, and
you will see
it does not allow to convert boolean to enumeration.

Try exactly what I wrote.
 
Bill McCarthy said:
Try exactly what I wrote.

Sorry, I should have checked operator order of precedence:

If (enum_notifyFilter And NotifyFilters.Attributes) =
NotifyFilters.Attributes Then
 
null said:
I am using the NotifyFilters enumeration type. In 'integer' the values
are ored together, and I can use AND to check for equality like so

If bNotifyFilter And NotifyFilters.Attributes Then
However, when option strict is on this does not work, any ideas?

'If CBool(bNotifyFilter And NotifyFilters.Attributes) Then...'.
 
Herfried K. Wagner said:
'If CBool(bNotifyFilter And NotifyFilters.Attributes) Then...'.

Although that may work in this case because NotifyFilters.Attributes has the
value of 4, in general you should avoid doing this. Consider the case when
bNotifyFilter is 1 and lets say NotifyFilters.Attributes was 3, the result
of And'ing then would be 1, and hence non zero and hence True even though
the value bNotifyFilter doesn't actually contain 3. The safe way,
regardless of what value is being tested is :

If (bNotifyFilter And NotifyFilters.Attributes) = NotifyFilters.Attributes
Then
 
Sorry, I should have checked operator order of precedence:

If (enum_notifyFilter And NotifyFilters.Attributes) =
NotifyFilters.Attributes Then

Ok ... I see now
Thanks
 
I am using the NotifyFilters enumeration type. In 'integer' the values
'If CBool(bNotifyFilter And NotifyFilters.Attributes) Then...'.

I can see that that would work too, thanks.
 
null said:
I can see that that would work too, thanks.

Make sure you read Bill's comment -- this will only work if you are checking
only for the presence of a single flag.
 
I am using the NotifyFilters enumeration type. In 'integer' the values
are ored together, and I can use AND to check for equality like so

If bNotifyFilter And NotifyFilters.Attributes Then
However, when option strict is on this does not work, any ideas?

If (bNotifyFilter And NotifyFilters.Attributes) <> 0 Then
 
I am using the NotifyFilters enumeration type. In 'integer' the
Although that may work in this case because NotifyFilters.Attributes has the
value of 4, in general you should avoid doing this. Consider the case when
bNotifyFilter is 1 and lets say NotifyFilters.Attributes was 3, the result
of And'ing then would be 1, and hence non zero and hence True even though
the value bNotifyFilter doesn't actually contain 3. The safe way,
regardless of what value is being tested is :

If (bNotifyFilter And NotifyFilters.Attributes) = NotifyFilters.Attributes
Then

Why isn't there simply a "belongs to" operator, this seems to be the most
natural thing for flags-like enumerations. It seems that the 'AND' operator
necessitates knowledge of the implementation of the enumeration, which
is a loss of abstraction?
 
null said:
Why isn't there simply a "belongs to" operator, this seems to be the
most natural thing for flags-like enumerations. It seems that the
'AND' operator necessitates knowledge of the implementation of the
enumeration, which
is a loss of abstraction?


I agree there should be a simpler keyword. I thought Contains, e.g :
If bNotifyFilter Contains NotifyFilters.Attributes Then
 
Back
Top