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 2690
Hi Randy
"Randy" <ra***@penHATES PAMsoft.com> wrote in message
news:OF******** *****@TK2MSFTNG P11.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.Cop y 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***@penHATES PAMsoft.com> wrote in message
news:OF******** *****@TK2MSFTNG P11.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.Cop y 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***@penHATES PAMsoft.com> wrote in message
news:OF******** *****@TK2MSFTNG P11.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********@ema il.msn.com> wrote in message
news:Oy******** *****@TK2MSFTNG P09.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.Cop y 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***@penHATES PAMsoft.com> wrote in message news:OF******** *****@TK2MSFTNG P11.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********@ema il.msn.com> wrote in message
news:Oy******** *****@TK2MSFTNG P09.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.Cop y 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***@penHATES PAMsoft.com> wrote in message news:OF******** *****@TK2MSFTNG P11.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******** ********@TK2MSF TNGP12.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********@ema il.msn.com> wrote in
message news:Oy******** *****@TK2MSFTNG P09.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.Cop y 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***@penHATES PAMsoft.com> wrote in message news:OF******** *****@TK2MSFTNG P11.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******** ********@TK2MSF TNGP12.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********@ema il.msn.com> wrote in
message news:Oy******** *****@TK2MSFTNG P09.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.Cop y 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***@penHATES PAMsoft.com> wrote in message news:OF******** *****@TK2MSFTNG P11.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:
public enum xColors {
Red = 1,
|
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 compiles fine. What
gives?
#pragma once
using namespace System;
namespace TestA
|
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 enum's when omitted.
<Flags()> Public Enum Permission
Read
Write
Delete
Rename
Copy
|
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 EnumName As String, ByVal args As Hashtable) As Type
Dim ad As AppDomain
Dim an As New AssemblyName
Dim ab As AssemblyBuilder
Dim mb As ModuleBuilder
Dim eb As EnumBuilder
Dim t As Type
| |
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 pipe symbol .for
example you can use something like this
AddRole("My Group", "Test", 0x10000000|0x00000002);
|
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 snippet that I wrote today that gives me
an instant implementation of the pattern. I could easily just always
use such an implementation instead of a standard enum, so I wanted to
know what you experts all thought.
Is there a case for standard enums?
|
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 that you need an explicit cast from an int to get
a wrong value into an enum.
class Program
{
enum RGB0
{
|
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
{
SunRoof = 0x01,
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |