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? 5 4401
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?
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?
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?
<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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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: 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: 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...
|
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: 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 =...
|
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...
|
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: 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...
|
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. ...
| |
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
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...
|
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...
| |