473,626 Members | 3,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FlagsAttribute

If I dont specify the [Flags] 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?
Nov 15 '05 #1
5 4407
Hi,

No.
FlagsAttribute has influence only on output, check this code (toggle the
Flags attribute to see the difference):
class Class1

{

[Flags]

public enum Flagator

{

One = 1,

Two = 2

}

[STAThread]

static void Main(string[] args)

{

Flagator ft = Flagator.One | Flagator.Two;

Console.WriteLi ne(ft.ToString( ));

Console.ReadLin e();

}

}
--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

<di********@dis cussion.microso ft.com> wrote in message
news:OD******** ******@TK2MSFTN GP11.phx.gbl...
If I dont specify the [Flags] 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?

Nov 15 '05 #2
Ok so it just affects the ToString() method?

I thought it affected the & and | operators.

"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:uv******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

No.
FlagsAttribute has influence only on output, check this code (toggle the
Flags attribute to see the difference):
class Class1

{

[Flags]

public enum Flagator

{

One = 1,

Two = 2

}

[STAThread]

static void Main(string[] args)

{

Flagator ft = Flagator.One | Flagator.Two;

Console.WriteLi ne(ft.ToString( ));

Console.ReadLin e();

}

}
--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

<di********@dis cussion.microso ft.com> wrote in message
news:OD******** ******@TK2MSFTN GP11.phx.gbl...
If I dont specify the [Flags] 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?


Nov 15 '05 #3
As far as the math goes, no. The bitwise operators will still work
correctly. However, the Flags attribute changes the behaviour of ToString()
so that it displays the proper set(all the set flags) instead of simply one
or a simple number.

There is no guarentee that bitwise operators will work on enums without
flags in other languages.

<di********@dis cussion.microso ft.com> wrote in message
news:OD******** ******@TK2MSFTN GP11.phx.gbl...
If I dont specify the [Flags] 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?

Nov 15 '05 #4
<di********@dis cussion.microso ft.com> wrote:
Ok so it just affects the ToString() method?

I thought it affected the & and | operators.


Not in C#, but another language *may* decide to only allow the & and |
operators to work with enums declared as Flags.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
I think thats how it should be for the operators, it makes no sense to use
bitwise operators on a non bitwise type.

I would also like to see some form of auto enumeration by base as an
attribute to it can automatically fill in 0,1,2,4,8 based on the order
unless I specify otherwise.

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<di********@dis cussion.microso ft.com> wrote:
Ok so it just affects the ToString() method?

I thought it affected the & and | operators.


Not in C#, but another language *may* decide to only allow the & and |
operators to work with enums declared as Flags.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2691
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
0
1373
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,
5
1302
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
2
1681
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
15
3070
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);
2
886
by: Charles Jenkins | last post by:
I want to know if FontStyle.Bold is set in a font's Style property. MS's help file is full of lovely examples of how to OR flags together in order to set the property, but as usual, seems to be missing the other half of the equation: How do you test the darned flags? These tests all give me a compiler errors in C#: FontStyle fs = myFont.Style; bool isBold; isBold = (( fs & FontStyle.Bold ) == FontStyle.Bold );
34
11174
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?
2
2046
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 {
5
5040
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,
0
8266
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8199
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,...
0
8705
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
jinu1996
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...
0
8505
tracyyun
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...
0
7196
agi2029
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...
1
6125
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2626
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
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.