I can't seem to find a clear example on how to use the FlagsAttribute. In
my example below shouldn't both message boxes show? Instead no messagebox
displays. I know that .NET will auto-number enum's when omitted.
<Flags()> Public Enum Permission
Read
Write
Delete
Rename
Copy
Move
End Enum
Private p As Permission
p = Permission.Copy And Permission.Read
If p = Permission.Copy Then
MessageBox.Show("Copy")
End If
If p = Permission.Copy And p = Permission.Read Then
MessageBox.Show("Copy & Read")
End If 7 2678
Hi Randy
"Randy" <ra***@penHATESPAMsoft.com> wrote in message
news:OF*************@TK2MSFTNGP11.phx.gbl... I can't seem to find a clear example on how to use the FlagsAttribute. In my example below shouldn't both message boxes show? Instead no messagebox displays. I know that .NET will auto-number enum's when omitted.
<snip>
If you don't specify values for your enum fields, they will be assigned
values in a straight sequence (1,2,3,4, etc...). If you want a "flagged"
enum you need to specify values that correspond to individual bit positions
(1,2,4,16, etc.):
<Flags()> Public Enum Permission
Read = 1
Write = 2
Delete = 4
Rename = 16
Copy = 32
Move = 64
End Enum
Hope that helps.
Regards,
Dan
p.s. *Should* the compiler be smart enough to auto-assign values correctly
if the "Flags" attribute is present? I think so.
Randy,
In addition to Daniel's suggestion of giving values to the Enum values.
<Flags()> Public Enum Permission
Read = 1
Write = 2
Delete = 4
Rename = 16
Copy = 32
Move = 64
End Enum
To combine the values you need to use Or. Private p As Permission
p = Permission.Copy Or Permission.Read
Then you need to 'mask' the value and see if its not zero.
If (p and Permission.Copy) <> 0 Then MessageBox.Show("Copy") End If
You can check for not zero or both values combined:
If (p and Permission.Copy Or Permission.Read) = (Permission.Copy Or
Permission.Read) Then MessageBox.Show("Copy & Read") End If
Sometimes when dealing with combined values, such as Read & Copy above, I
will define a constant that represents the two values combined
Const ReadAndCopy As Permission = Permission.Copy Or Permission.Read
If (p and ReadAndCopy ) = ReadAndCopy Then MessageBox.Show("Copy & Read") End If
Other times I will setup a function to check the masked values:
Private Function CheckMask(ByVal value As Permission, ByVal mask As
Permission)
Return (value And mask) = value
End Function
If CheckMask(p, Permission.Copy Or Permission.Read) Then MessageBox.Show("Copy & Read") End If
I will use the mask & value method when I want to check that out of Copy &
Read only Copy is set.
If (p and Permission.Copy Or Permission.Read) = Permission.Copy Then
MessageBox.Show("Of Copy & Read, only Copy set")
End If
When you think about it, one would expect And would be used to combine the
value (set theory). However enums are implemented as integers and boolean
theory is how they are worked with...
Hope this helps
Jay
"Randy" <ra***@penHATESPAMsoft.com> wrote in message
news:OF*************@TK2MSFTNGP11.phx.gbl... I can't seem to find a clear example on how to use the FlagsAttribute. In my example below shouldn't both message boxes show? Instead no messagebox displays. I know that .NET will auto-number enum's when omitted.
<Flags()> Public Enum Permission Read Write Delete Rename Copy Move End Enum
Private p As Permission p = Permission.Copy And Permission.Read
If p = Permission.Copy Then MessageBox.Show("Copy") End If
If p = Permission.Copy And p = Permission.Read Then MessageBox.Show("Copy & Read") End If
Randy,
In addition to Daniel's suggestion of giving values to the Enum values.
<Flags()> Public Enum Permission
Read = 1
Write = 2
Delete = 4
Rename = 16
Copy = 32
Move = 64
End Enum
To combine the values you need to use Or. Private p As Permission
p = Permission.Copy Or Permission.Read
Then you need to 'mask' the value and see if its not zero.
If (p and Permission.Copy) <> 0 Then MessageBox.Show("Copy") End If
You can check for not zero or both values combined:
If (p and Permission.Copy Or Permission.Read) = (Permission.Copy Or
Permission.Read) Then MessageBox.Show("Copy & Read") End If
Sometimes when dealing with combined values, such as Read & Copy above, I
will define a constant that represents the two values combined
Const ReadAndCopy As Permission = Permission.Copy Or Permission.Read
If (p and ReadAndCopy ) = ReadAndCopy Then MessageBox.Show("Copy & Read") End If
Other times I will setup a function to check the masked values:
Private Function CheckMask(ByVal value As Permission, ByVal mask As
Permission)
Return (value And mask) = value
End Function
If CheckMask(p, Permission.Copy Or Permission.Read) Then MessageBox.Show("Copy & Read") End If
I will use the mask & value method when I want to check that out of Copy &
Read only Copy is set.
If (p and Permission.Copy Or Permission.Read) = Permission.Copy Then
MessageBox.Show("Of Copy & Read, only Copy set")
End If
When you think about it, one would expect And would be used to combine the
value (set theory). However enums are implemented as integers and boolean
theory is how they are worked with...
Hope this helps
Jay
"Randy" <ra***@penHATESPAMsoft.com> wrote in message
news:OF*************@TK2MSFTNGP11.phx.gbl... I can't seem to find a clear example on how to use the FlagsAttribute. In my example below shouldn't both message boxes show? Instead no messagebox displays. I know that .NET will auto-number enum's when omitted.
<Flags()> Public Enum Permission Read Write Delete Rename Copy Move End Enum
Private p As Permission p = Permission.Copy And Permission.Read
If p = Permission.Copy Then MessageBox.Show("Copy") End If
If p = Permission.Copy And p = Permission.Read Then MessageBox.Show("Copy & Read") End If
This is something I find that .NET messed up.
if I specify the [Flags] attribute, why should I have to set the enum values
in powers of 2? Why cant the enum be smart enough to see that attribute and
set them accordingly. What is the benifit of the Flags attribute then? Its
lost. Pointless.
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:Oy*************@TK2MSFTNGP09.phx.gbl... Randy, In addition to Daniel's suggestion of giving values to the Enum values.
<Flags()> Public Enum Permission Read = 1 Write = 2 Delete = 4 Rename = 16 Copy = 32 Move = 64 End Enum
To combine the values you need to use Or.
Private p As Permission p = Permission.Copy Or Permission.Read
Then you need to 'mask' the value and see if its not zero.
If (p and Permission.Copy) <> 0 Then MessageBox.Show("Copy") End If
You can check for not zero or both values combined: If (p and Permission.Copy Or Permission.Read) = (Permission.Copy Or Permission.Read) Then MessageBox.Show("Copy & Read") End If
Sometimes when dealing with combined values, such as Read & Copy above, I will define a constant that represents the two values combined
Const ReadAndCopy As Permission = Permission.Copy Or Permission.Read If (p and ReadAndCopy ) = ReadAndCopy Then MessageBox.Show("Copy & Read") End If
Other times I will setup a function to check the masked values:
Private Function CheckMask(ByVal value As Permission, ByVal mask As Permission) Return (value And mask) = value End Function
If CheckMask(p, Permission.Copy Or Permission.Read) Then MessageBox.Show("Copy & Read") End If
I will use the mask & value method when I want to check that out of Copy & Read only Copy is set.
If (p and Permission.Copy Or Permission.Read) = Permission.Copy Then MessageBox.Show("Of Copy & Read, only Copy set") End If
When you think about it, one would expect And would be used to combine the value (set theory). However enums are implemented as integers and boolean theory is how they are worked with...
Hope this helps Jay
"Randy" <ra***@penHATESPAMsoft.com> wrote in message news:OF*************@TK2MSFTNGP11.phx.gbl... I can't seem to find a clear example on how to use the FlagsAttribute.
In my example below shouldn't both message boxes show? Instead no
messagebox displays. I know that .NET will auto-number enum's when omitted.
<Flags()> Public Enum Permission Read Write Delete Rename Copy Move End Enum
Private p As Permission p = Permission.Copy And Permission.Read
If p = Permission.Copy Then MessageBox.Show("Copy") End If
If p = Permission.Copy And p = Permission.Read Then MessageBox.Show("Copy & Read") End If
This is something I find that .NET messed up.
if I specify the [Flags] attribute, why should I have to set the enum values
in powers of 2? Why cant the enum be smart enough to see that attribute and
set them accordingly. What is the benifit of the Flags attribute then? Its
lost. Pointless.
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:Oy*************@TK2MSFTNGP09.phx.gbl... Randy, In addition to Daniel's suggestion of giving values to the Enum values.
<Flags()> Public Enum Permission Read = 1 Write = 2 Delete = 4 Rename = 16 Copy = 32 Move = 64 End Enum
To combine the values you need to use Or.
Private p As Permission p = Permission.Copy Or Permission.Read
Then you need to 'mask' the value and see if its not zero.
If (p and Permission.Copy) <> 0 Then MessageBox.Show("Copy") End If
You can check for not zero or both values combined: If (p and Permission.Copy Or Permission.Read) = (Permission.Copy Or Permission.Read) Then MessageBox.Show("Copy & Read") End If
Sometimes when dealing with combined values, such as Read & Copy above, I will define a constant that represents the two values combined
Const ReadAndCopy As Permission = Permission.Copy Or Permission.Read If (p and ReadAndCopy ) = ReadAndCopy Then MessageBox.Show("Copy & Read") End If
Other times I will setup a function to check the masked values:
Private Function CheckMask(ByVal value As Permission, ByVal mask As Permission) Return (value And mask) = value End Function
If CheckMask(p, Permission.Copy Or Permission.Read) Then MessageBox.Show("Copy & Read") End If
I will use the mask & value method when I want to check that out of Copy & Read only Copy is set.
If (p and Permission.Copy Or Permission.Read) = Permission.Copy Then MessageBox.Show("Of Copy & Read, only Copy set") End If
When you think about it, one would expect And would be used to combine the value (set theory). However enums are implemented as integers and boolean theory is how they are worked with...
Hope this helps Jay
"Randy" <ra***@penHATESPAMsoft.com> wrote in message news:OF*************@TK2MSFTNGP11.phx.gbl... I can't seem to find a clear example on how to use the FlagsAttribute.
In my example below shouldn't both message boxes show? Instead no
messagebox displays. I know that .NET will auto-number enum's when omitted.
<Flags()> Public Enum Permission Read Write Delete Rename Copy Move End Enum
Private p As Permission p = Permission.Copy And Permission.Read
If p = Permission.Copy Then MessageBox.Show("Copy") End If
If p = Permission.Copy And p = Permission.Read Then MessageBox.Show("Copy & Read") End If
Mr.Tickle,
I won't disagree with you in that the compiler should figure out to use
Powers of two when you apply the Flags attribute and do not explicitly list
the values. What is the benifit of the Flags attribute then?
The attribute controls the default behavior of the Enum.ToString method.
For example:
Private p As Permission
p = Permission.Copy Or Permission.Read
Debug.WriteLine(p, "Permission")
If Permission has the Flags attribute, then "Permission: Copy, Read" will be
printed, without the Flags attribute 33 will be printed. You can use one of
the Overloaded Enum.ToString methods to control this...
Hope this helps
Jay
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... This is something I find that .NET messed up.
if I specify the [Flags] attribute, why should I have to set the enum
values in powers of 2? Why cant the enum be smart enough to see that attribute
and set them accordingly. What is the benifit of the Flags attribute then?
Its lost. Pointless.
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in
message news:Oy*************@TK2MSFTNGP09.phx.gbl... Randy, In addition to Daniel's suggestion of giving values to the Enum values.
<Flags()> Public Enum Permission Read = 1 Write = 2 Delete = 4 Rename = 16 Copy = 32 Move = 64 End Enum
To combine the values you need to use Or.
Private p As Permission p = Permission.Copy Or Permission.Read
Then you need to 'mask' the value and see if its not zero.
If (p and Permission.Copy) <> 0 Then MessageBox.Show("Copy") End If
You can check for not zero or both values combined: If (p and Permission.Copy Or Permission.Read) = (Permission.Copy Or Permission.Read) Then MessageBox.Show("Copy & Read") End If
Sometimes when dealing with combined values, such as Read & Copy above,
I will define a constant that represents the two values combined
Const ReadAndCopy As Permission = Permission.Copy Or Permission.Read If (p and ReadAndCopy ) = ReadAndCopy Then MessageBox.Show("Copy & Read") End If
Other times I will setup a function to check the masked values:
Private Function CheckMask(ByVal value As Permission, ByVal mask As Permission) Return (value And mask) = value End Function
If CheckMask(p, Permission.Copy Or Permission.Read) Then MessageBox.Show("Copy & Read") End If
I will use the mask & value method when I want to check that out of Copy
& Read only Copy is set.
If (p and Permission.Copy Or Permission.Read) = Permission.Copy
Then MessageBox.Show("Of Copy & Read, only Copy set") End If
When you think about it, one would expect And would be used to combine
the value (set theory). However enums are implemented as integers and
boolean theory is how they are worked with...
Hope this helps Jay
"Randy" <ra***@penHATESPAMsoft.com> wrote in message news:OF*************@TK2MSFTNGP11.phx.gbl... I can't seem to find a clear example on how to use the FlagsAttribute. In my example below shouldn't both message boxes show? Instead no messagebox displays. I know that .NET will auto-number enum's when omitted.
<Flags()> Public Enum Permission Read Write Delete Rename Copy Move End Enum
Private p As Permission p = Permission.Copy And Permission.Read
If p = Permission.Copy Then MessageBox.Show("Copy") End If
If p = Permission.Copy And p = Permission.Read Then MessageBox.Show("Copy & Read") End If
Mr.Tickle,
I won't disagree with you in that the compiler should figure out to use
Powers of two when you apply the Flags attribute and do not explicitly list
the values. What is the benifit of the Flags attribute then?
The attribute controls the default behavior of the Enum.ToString method.
For example:
Private p As Permission
p = Permission.Copy Or Permission.Read
Debug.WriteLine(p, "Permission")
If Permission has the Flags attribute, then "Permission: Copy, Read" will be
printed, without the Flags attribute 33 will be printed. You can use one of
the Overloaded Enum.ToString methods to control this...
Hope this helps
Jay
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... This is something I find that .NET messed up.
if I specify the [Flags] attribute, why should I have to set the enum
values in powers of 2? Why cant the enum be smart enough to see that attribute
and set them accordingly. What is the benifit of the Flags attribute then?
Its lost. Pointless.
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in
message news:Oy*************@TK2MSFTNGP09.phx.gbl... Randy, In addition to Daniel's suggestion of giving values to the Enum values.
<Flags()> Public Enum Permission Read = 1 Write = 2 Delete = 4 Rename = 16 Copy = 32 Move = 64 End Enum
To combine the values you need to use Or.
Private p As Permission p = Permission.Copy Or Permission.Read
Then you need to 'mask' the value and see if its not zero.
If (p and Permission.Copy) <> 0 Then MessageBox.Show("Copy") End If
You can check for not zero or both values combined: If (p and Permission.Copy Or Permission.Read) = (Permission.Copy Or Permission.Read) Then MessageBox.Show("Copy & Read") End If
Sometimes when dealing with combined values, such as Read & Copy above,
I will define a constant that represents the two values combined
Const ReadAndCopy As Permission = Permission.Copy Or Permission.Read If (p and ReadAndCopy ) = ReadAndCopy Then MessageBox.Show("Copy & Read") End If
Other times I will setup a function to check the masked values:
Private Function CheckMask(ByVal value As Permission, ByVal mask As Permission) Return (value And mask) = value End Function
If CheckMask(p, Permission.Copy Or Permission.Read) Then MessageBox.Show("Copy & Read") End If
I will use the mask & value method when I want to check that out of Copy
& Read only Copy is set.
If (p and Permission.Copy Or Permission.Read) = Permission.Copy
Then MessageBox.Show("Of Copy & Read, only Copy set") End If
When you think about it, one would expect And would be used to combine
the value (set theory). However enums are implemented as integers and
boolean theory is how they are worked with...
Hope this helps Jay
"Randy" <ra***@penHATESPAMsoft.com> wrote in message news:OF*************@TK2MSFTNGP11.phx.gbl... I can't seem to find a clear example on how to use the FlagsAttribute. In my example below shouldn't both message boxes show? Instead no messagebox displays. I know that .NET will auto-number enum's when omitted.
<Flags()> Public Enum Permission Read Write Delete Rename Copy Move End Enum
Private p As Permission p = Permission.Copy And Permission.Read
If p = Permission.Copy Then MessageBox.Show("Copy") End If
If p = Permission.Copy And p = Permission.Read Then MessageBox.Show("Copy & Read") End If
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: |
last post by:
If I dont specify the attribute on an enum to be used as a bitfield
declared as 0,1,2,4,8... and so on, and use it as one normally would with |
and & operators, would it behave differently?
|
by: Horia Tudosie |
last post by:
Using Visual Studio 2003
This is to report a series of bugs regarding the FlagsAttribute and
(independently) the usage of interfaces in Web applications.
Let’s declare xColors type like:
...
|
by: Ken Varn |
last post by:
The following code snippet will not compile. I get an error C2337 saying
that the flags attribute is not found, however, if I remove the last
namespace System from the namespace tree, then it...
|
by: Randy |
last post by:
I can't seem to find a clear example on how to use the FlagsAttribute. In
my example below shouldn't both message boxes show? Instead no messagebox
displays. I know that .NET will auto-number...
|
by: Nicolas |
last post by:
How do I set a FlagsAttribute to a run time created enum
I really need a FlagsAttribute for the enum as it will be use to trigger multiple choice
Thanks for the help
Function BuildEnum(ByVal...
|
by: Fariba |
last post by:
Hello ,
I am trying to call a mthod with the following signature:
AddRole(string Group_Nam, string Description, int permissionmask);
Accroding to msdn ,you can mask the permissions using...
|
by: Steven Nagy |
last post by:
So I was needing some extra power from my enums and implemented the
typesafe enum pattern.
And it got me to thinking... why should I EVER use standard enums?
There's now a nice little code...
|
by: Zytan |
last post by:
I know you can use enums as a bit field with , but,
when I use an enum without , I can still combine one
or more of them. There's no error in doing so. I thought enums were
strongly typed, such...
|
by: rajanipro |
last post by:
Please explain to me the following C# program, which I came across in MSDN help:
// enumFlags.cs
// Using the FlagsAttribute on enumerations.
using System;
public enum CarOptions
{
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |