473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

// Run this code and look for his obvious error by the compiler
namespace FlagCheck
{
using System;

[Flags]
enum SomeFlags
{
None = 0,
One,
Two,
Three,
Four,
Five,
Six,
All = One | Two | Three | Four | Five | Six
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
SomeFlags f;

f = SomeFlags.Four; // Set flag FOUR and ONLY FOUR, get ready for a
supprise

if ( (f & SomeFlags.One) > 0)
{
Console.WriteLine("flag one is set");
}

if ( (f & SomeFlags.Two) > 0)
{
Console.WriteLine("flag two is set");
}

if ( (f & SomeFlags.Three) > 0)
{
Console.WriteLine("flag three is set");
}

if ( (f & SomeFlags.Four) > 0)
{
Console.WriteLine("flag four is set");
}

if ( (f & SomeFlags.Five) > 0)
{
Console.WriteLine("flag five is set");
}

if ( (f & SomeFlags.Six) > 0)
{
Console.WriteLine("flag six is set");
}
Console.ReadLine();
}
}
}
Nov 15 '05 #1
43 1504
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:ee**************@TK2MSFTNGP11.phx.gbl...
// Run this code and look for his obvious error by the compiler


What you wrote and what you meant are two different things. The compiler is
fine.

You have:
enum SomeFlags
{
None = 0, // 0000
One, // 0001
Two, // 0010
Three, // 0011
Four, // 0100
Five, // 0101
Six, // 0110
All = One | Two | Three | Four | Five | Six // 0111
};

You meant:
enum SomeFlags
{
None = 0,
One = 1,
Two = 2,
Three = 4,
Four = 8,
Five = 16,
Six = 32,
All = One | Two | Three | Four | Five | Six
}

Change your code and it works fine.

-- Alan
Nov 15 '05 #2
Works fine for me.

The out put is:

flag four is set
flag five is set
flag six is set

as expected.
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:ee**************@TK2MSFTNGP11.phx.gbl...
// Run this code and look for his obvious error by the compiler
namespace FlagCheck
{
using System;

[Flags]
enum SomeFlags
{
None = 0,
One,
Two,
Three,
Four,
Five,
Six,
All = One | Two | Three | Four | Five | Six
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
SomeFlags f;

f = SomeFlags.Four; // Set flag FOUR and ONLY FOUR, get ready for a
supprise

if ( (f & SomeFlags.One) > 0)
{
Console.WriteLine("flag one is set");
}

if ( (f & SomeFlags.Two) > 0)
{
Console.WriteLine("flag two is set");
}

if ( (f & SomeFlags.Three) > 0)
{
Console.WriteLine("flag three is set");
}

if ( (f & SomeFlags.Four) > 0)
{
Console.WriteLine("flag four is set");
}

if ( (f & SomeFlags.Five) > 0)
{
Console.WriteLine("flag five is set");
}

if ( (f & SomeFlags.Six) > 0)
{
Console.WriteLine("flag six is set");
}
Console.ReadLine();
}
}
}

Nov 15 '05 #3
It should be powers of 2 by default for flags.

Thats my point.
"Alan Pretre" <no@spam> wrote in message
news:#s**************@TK2MSFTNGP11.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:ee**************@TK2MSFTNGP11.phx.gbl...
// Run this code and look for his obvious error by the compiler
What you wrote and what you meant are two different things. The compiler

is fine.

You have:
enum SomeFlags
{
None = 0, // 0000
One, // 0001
Two, // 0010
Three, // 0011
Four, // 0100
Five, // 0101
Six, // 0110
All = One | Two | Three | Four | Five | Six // 0111
};

You meant:
enum SomeFlags
{
None = 0,
One = 1,
Two = 2,
Three = 4,
Four = 8,
Five = 16,
Six = 32,
All = One | Two | Three | Four | Five | Six
}

Change your code and it works fine.

-- Alan

Nov 15 '05 #4
WRONG, its a Flag, should be powers of 2. you ninney
You expected wrong.
"Stephen Martin" <sm*****@removethis.emsoft.andthis.ca> wrote in message
news:u5*************@TK2MSFTNGP10.phx.gbl...
Works fine for me.

The out put is:

flag four is set
flag five is set
flag six is set

as expected.
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:ee**************@TK2MSFTNGP11.phx.gbl...
// Run this code and look for his obvious error by the compiler
namespace FlagCheck
{
using System;

[Flags]
enum SomeFlags
{
None = 0,
One,
Two,
Three,
Four,
Five,
Six,
All = One | Two | Three | Four | Five | Six
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
SomeFlags f;

f = SomeFlags.Four; // Set flag FOUR and ONLY FOUR, get ready for a
supprise

if ( (f & SomeFlags.One) > 0)
{
Console.WriteLine("flag one is set");
}

if ( (f & SomeFlags.Two) > 0)
{
Console.WriteLine("flag two is set");
}

if ( (f & SomeFlags.Three) > 0)
{
Console.WriteLine("flag three is set");
}

if ( (f & SomeFlags.Four) > 0)
{
Console.WriteLine("flag four is set");
}

if ( (f & SomeFlags.Five) > 0)
{
Console.WriteLine("flag five is set");
}

if ( (f & SomeFlags.Six) > 0)
{
Console.WriteLine("flag six is set");
}
Console.ReadLine();
}
}
}


Nov 15 '05 #5
What i meant was bloody obvious and expected as its a FLAG, why the hell
would I preform base 10 operations on a base 2 type (Flag)?
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:uP**************@TK2MSFTNGP12.phx.gbl...
It should be powers of 2 by default for flags.

Thats my point.
"Alan Pretre" <no@spam> wrote in message
news:#s**************@TK2MSFTNGP11.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:ee**************@TK2MSFTNGP11.phx.gbl...
// Run this code and look for his obvious error by the compiler
What you wrote and what you meant are two different things. The

compiler is
fine.

You have:
enum SomeFlags
{
None = 0, // 0000
One, // 0001
Two, // 0010
Three, // 0011
Four, // 0100
Five, // 0101
Six, // 0110
All = One | Two | Three | Four | Five | Six // 0111
};

You meant:
enum SomeFlags
{
None = 0,
One = 1,
Two = 2,
Three = 4,
Four = 8,
Five = 16,
Six = 32,
All = One | Two | Three | Four | Five | Six
}

Change your code and it works fine.

-- Alan


Nov 15 '05 #6
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:eK**************@TK2MSFTNGP10.phx.gbl...
WRONG, its a Flag, should be powers of 2. you ninney


Looks like someone needs a lesson about attributes. What a hoot. As if
attributes morph the language.

-- Alan
Nov 15 '05 #7
They influence the operators , why not the enum default values.

So in a way they do MORPH it. fagot.ur a hoot.
"Alan Pretre" <no@spam> wrote in message
news:#2**************@TK2MSFTNGP12.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:eK**************@TK2MSFTNGP10.phx.gbl...
WRONG, its a Flag, should be powers of 2. you ninney


Looks like someone needs a lesson about attributes. What a hoot. As if
attributes morph the language.

-- Alan

Nov 15 '05 #8
In article <eK**************@TK2MSFTNGP10.phx.gbl>, Mr******@mrmen.com
says...
WRONG, its a Flag, should be powers of 2


Check the documentation for the FlagsAttribute class:

"Indicates that an enumeration can be treated as a bit field"

It says it "can be treated" as a bit field. It does not mean the
enumeration "will be defined" as a bitfield.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 15 '05 #9
Thats my point, it should be. There is no reason to have it otherwise.

Logic dictates that it should be powers of 2, nothing else.
"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
In article <eK**************@TK2MSFTNGP10.phx.gbl>, Mr******@mrmen.com
says...
WRONG, its a Flag, should be powers of 2


Check the documentation for the FlagsAttribute class:

"Indicates that an enumeration can be treated as a bit field"

It says it "can be treated" as a bit field. It does not mean the
enumeration "will be defined" as a bitfield.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele

Nov 15 '05 #10
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:u0**************@TK2MSFTNGP12.phx.gbl...
They influence the operators , why not the enum default values.
So in a way they do MORPH it. fagot.ur a hoot.


Why don't you RTM and see what it does. Does it say that the FlagsAttribute
changes the way enum constants are defined? Hmmm? No it causes the
compiler to relax the restrictions on the bitwise OR operator, which would
otherwise not be allowed on the enum members.

-- Alan
Nov 15 '05 #11
Sure you can override it if you want other values, just like normal
behaviour today. but having an option to have it enumerate in powers of 2
would be handy as in my example. Isnt that the point of Enums, to
enumerate, in this case it enumerats incorrectly for bitfields on a base 2
system.

"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:#O**************@TK2MSFTNGP10.phx.gbl...
Thats my point, it should be. There is no reason to have it otherwise.

Logic dictates that it should be powers of 2, nothing else.
"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
In article <eK**************@TK2MSFTNGP10.phx.gbl>, Mr******@mrmen.com
says...
WRONG, its a Flag, should be powers of 2


Check the documentation for the FlagsAttribute class:

"Indicates that an enumeration can be treated as a bit field"

It says it "can be treated" as a bit field. It does not mean the
enumeration "will be defined" as a bitfield.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele


Nov 15 '05 #12
Sure, RTM RTM blah, suppose you say "Linux owns j000, go rtfm l0ser!"

u sad fuckk. My issue is, why cant it not have powers of 2 for default
enumerations on bitfields where it would be useful, for gawds sakes remove
yer head from yer arsee and think beyond the manual.

"Alan Pretre" <no@spam> wrote in message
news:eF**************@TK2MSFTNGP11.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:u0**************@TK2MSFTNGP12.phx.gbl...
They influence the operators , why not the enum default values.
So in a way they do MORPH it. fagot.ur a hoot.
Why don't you RTM and see what it does. Does it say that the

FlagsAttribute changes the way enum constants are defined? Hmmm? No it causes the
compiler to relax the restrictions on the bitwise OR operator, which would
otherwise not be allowed on the enum members.

-- Alan

Nov 15 '05 #13
I never said it changes the way theyre defined, thats what im asking for.

Do you ever think BEYOND the manual? Guess you are one of those backroom
uncreative programmers that just copies from a manual and CANT THINK FOR
YERSELF OR FURTHER.

"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:up**************@TK2MSFTNGP12.phx.gbl...
Sure, RTM RTM blah, suppose you say "Linux owns j000, go rtfm l0ser!"

u sad fuckk. My issue is, why cant it not have powers of 2 for default
enumerations on bitfields where it would be useful, for gawds sakes remove
yer head from yer arsee and think beyond the manual.

"Alan Pretre" <no@spam> wrote in message
news:eF**************@TK2MSFTNGP11.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:u0**************@TK2MSFTNGP12.phx.gbl...
They influence the operators , why not the enum default values.
So in a way they do MORPH it. fagot.ur a hoot.


Why don't you RTM and see what it does. Does it say that the

FlagsAttribute
changes the way enum constants are defined? Hmmm? No it causes the
compiler to relax the restrictions on the bitwise OR operator, which would otherwise not be allowed on the enum members.

-- Alan


Nov 15 '05 #14
Would it not be useful to be able specify an increment like powers of 2 for
bitfields, or any other increment somehow? Why must it always be one, I know
what the damnn manual says, dont quote me on that crap, I am thinking OUT OF
THE BOX (something which alot of you lot have yet to experience)

"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
In article <eK**************@TK2MSFTNGP10.phx.gbl>, Mr******@mrmen.com
says...
WRONG, its a Flag, should be powers of 2


Check the documentation for the FlagsAttribute class:

"Indicates that an enumeration can be treated as a bit field"

It says it "can be treated" as a bit field. It does not mean the
enumeration "will be defined" as a bitfield.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele

Nov 15 '05 #15
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Sure you can override it if you want other values, just like normal
behaviour today. but having an option to have it enumerate in powers of 2
would be handy as in my example. Isnt that the point of Enums, to
enumerate, in this case it enumerats incorrectly for bitfields on a base 2
system.


The C# language spec says, in 14.3:

"...the associated value of the enum member is obtained by ___increasing
the associated value of the textually preceding enum member by one___. This
increased value must be within the range of values that can be represented
by the underlying type; otherwise, a compile-time error occurs. "

As Patrick says, various CLR languages may treat the attribute differently.
In my tests I couldn't see any difference in behavior with the
FlagsAttribute in C# (what I mean is whether present or not, C# seemed to
function the same.)

To return to your OP, I think the only one surprised was you.

-- Alan
Nov 15 '05 #16
Alan Pretre <no@spam> wrote:
Why don't you RTM and see what it does. Does it say that the FlagsAttribute
changes the way enum constants are defined? Hmmm? No it causes the
compiler to relax the restrictions on the bitwise OR operator, which would
otherwise not be allowed on the enum members.


In fact, it doesn't even do that - you can use the bitwise OR operator
even on enumerations which *don't* have the [Flags] attribute - at
least you can in C#.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #17
No i wasnt supprised. I was justing questioning the status quo, why must it
be inrements of ONE in the standard, why not have this definable in the
attribute. Then it would be useful to specify an increment of powers of 2
for bitfields, I know what the specs say, I am just trying pointing out a
case where it would be handy to have a different increment. Why must always
the lame people that cant think out of the box always run off to the manuals
for backup quotes. Try thinking out of the box. I guess you are one of
those people that never actually made a DIFFERENCE; you are just a
FOLLOWER.Nuff said really.

"Alan Pretre" <no@spam> wrote in message
news:e2**************@TK2MSFTNGP11.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Sure you can override it if you want other values, just like normal
behaviour today. but having an option to have it enumerate in powers of 2 would be handy as in my example. Isnt that the point of Enums, to
enumerate, in this case it enumerats incorrectly for bitfields on a base 2 system.
The C# language spec says, in 14.3:

"...the associated value of the enum member is obtained by ___increasing
the associated value of the textually preceding enum member by one___.

This increased value must be within the range of values that can be represented
by the underlying type; otherwise, a compile-time error occurs. "

As Patrick says, various CLR languages may treat the attribute differently. In my tests I couldn't see any difference in behavior with the
FlagsAttribute in C# (what I mean is whether present or not, C# seemed to
function the same.)

To return to your OP, I think the only one surprised was you.

-- Alan

Nov 15 '05 #18
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:uT**************@TK2MSFTNGP10.phx.gbl...
I guess you are one of
those people that never actually made a DIFFERENCE; you are just a
FOLLOWER.Nuff said really.


LOL you are new around here. Just ask me about deterministic destructors.

-- Alan
Nov 15 '05 #19
Why would I ask you, when I can ask Jack as Im sure he knows more than you.
If I wanted Jack's opinion I would ask Jack.
"Alan Pretre" <no@spam> wrote in message
news:uz**************@TK2MSFTNGP11.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:uT**************@TK2MSFTNGP10.phx.gbl...
I guess you are one of
those people that never actually made a DIFFERENCE; you are just a
FOLLOWER.Nuff said really.


LOL you are new around here. Just ask me about deterministic destructors.

-- Alan

Nov 15 '05 #20


If you cant see the application of specifying incrementts in enumerations,
you need glasses.
"Alan Pretre" <no@spam> wrote in message
news:uz**************@TK2MSFTNGP11.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:uT**************@TK2MSFTNGP10.phx.gbl...
I guess you are one of
those people that never actually made a DIFFERENCE; you are just a
FOLLOWER.Nuff said really.


LOL you are new around here. Just ask me about deterministic destructors.

-- Alan

Nov 15 '05 #21
100
Hi,
Flag attributes is used only by the methods that converts the value to text
(ToString method).
You can use bitwise oberators even if FlagAttribute is not set.
There is no restriction that FlagAttribute has to be used only with powers
of 2.

B\rgds
100
Nov 15 '05 #22
You started with "obvious error by the compiler", "Obvious mistake by Eric
Gunnarsson", "should be powers of two". After people pointed out that the
flag attribute is doing exactly what the spec says. You now changed your
tune to "I'm thinking outside the box" and "wouldn't it be handy to
increment of powers of 2". Which indicates you had no idea what the flag
attribute should have been doing to begin with, else you would have started
out the thread with "would be useful to specify an increment of powers of
2".
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:ee**************@TK2MSFTNGP11.phx.gbl...
// Run this code and look for his obvious error by the compiler
namespace FlagCheck
{
using System;

[Flags]
enum SomeFlags
{
None = 0,
One,
Two,
Three,
Four,
Five,
Six,
All = One | Two | Three | Four | Five | Six
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
SomeFlags f;

f = SomeFlags.Four; // Set flag FOUR and ONLY FOUR, get ready for a
supprise

if ( (f & SomeFlags.One) > 0)
{
Console.WriteLine("flag one is set");
}

if ( (f & SomeFlags.Two) > 0)
{
Console.WriteLine("flag two is set");
}

if ( (f & SomeFlags.Three) > 0)
{
Console.WriteLine("flag three is set");
}

if ( (f & SomeFlags.Four) > 0)
{
Console.WriteLine("flag four is set");
}

if ( (f & SomeFlags.Five) > 0)
{
Console.WriteLine("flag five is set");
}

if ( (f & SomeFlags.Six) > 0)
{
Console.WriteLine("flag six is set");
}
Console.ReadLine();
}
}
}

Nov 15 '05 #23
NoOne wrote:
You started with "obvious error by the compiler", "Obvious mistake by Eric
Gunnarsson", "should be powers of two". After people pointed out that the
flag attribute is doing exactly what the spec says. You now changed your
tune to "I'm thinking outside the box" and "wouldn't it be handy to
increment of powers of 2". Which indicates you had no idea what the flag
attribute should have been doing to begin with, else you would have started
out the thread with "would be useful to specify an increment of powers of
2".

Indeed. I actually think Mr. Tickle's idea is a good one. There is
ample precedent for attributes modifying compiler behavior.

However, I think that his name-calling approach of attempting to get his
point across is certainly counter productive.

Oh well, it's not like this is a gaping hole in the C# spec.

"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:ee**************@TK2MSFTNGP11.phx.gbl...
// Run this code and look for his obvious error by the compiler
namespace FlagCheck
{
using System;

[Flags]
enum SomeFlags
{
None = 0,
One,
Two,
Three,
Four,
Five,
Six,
All = One | Two | Three | Four | Five | Six
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
SomeFlags f;

f = SomeFlags.Four; // Set flag FOUR and ONLY FOUR, get ready for a
supprise

if ( (f & SomeFlags.One) > 0)
{
Console.WriteLine("flag one is set");
}

if ( (f & SomeFlags.Two) > 0)
{
Console.WriteLine("flag two is set");
}

if ( (f & SomeFlags.Three) > 0)
{
Console.WriteLine("flag three is set");
}

if ( (f & SomeFlags.Four) > 0)
{
Console.WriteLine("flag four is set");
}

if ( (f & SomeFlags.Five) > 0)
{
Console.WriteLine("flag five is set");
}

if ( (f & SomeFlags.Six) > 0)
{
Console.WriteLine("flag six is set");
}
Console.ReadLine();
}
}
}



--
mikeb

Nov 15 '05 #24
100
Hi Mr.Tickle.
To have powers of to as defalut for flags doesn't make sense to me.
Usualy flags provide constants often used combination of flags.
For example

enum Styles
{
WS_EX_WINDOWEDGE = 0x00000100L,
WS_EX_CLIENTEDGE = 0x00000200L,
WS_EX_OVERLAPPEDWINDOW = 0x00000300
}

WS_EX_OVERLAPPEDWINDOW (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE) = 768.
is it power of 2?

Some of the flags might have more the one bit set to 1 and thay might be not
power of 2. This doesn't makes them *less flags* than the others, though.

B\rgds
100
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:uU**************@TK2MSFTNGP11.phx.gbl...
What i meant was bloody obvious and expected as its a FLAG, why the hell
would I preform base 10 operations on a base 2 type (Flag)?
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:uP**************@TK2MSFTNGP12.phx.gbl...
It should be powers of 2 by default for flags.

Thats my point.
"Alan Pretre" <no@spam> wrote in message
news:#s**************@TK2MSFTNGP11.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:ee**************@TK2MSFTNGP11.phx.gbl...
> // Run this code and look for his obvious error by the compiler

What you wrote and what you meant are two different things. The

compiler
is
fine.

You have:
enum SomeFlags
{
None = 0, // 0000
One, // 0001
Two, // 0010
Three, // 0011
Four, // 0100
Five, // 0101
Six, // 0110
All = One | Two | Three | Four | Five | Six // 0111
};

You meant:
enum SomeFlags
{
None = 0,
One = 1,
Two = 2,
Three = 4,
Four = 8,
Five = 16,
Six = 32,
All = One | Two | Three | Four | Five | Six
}

Change your code and it works fine.

-- Alan



Nov 15 '05 #25
> However, I think that his name-calling approach of attempting to get his
point across is certainly counter productive.


The usual advice in such cases is "Do Not Feed The Troll". I think this most
definitely applies here.
Nov 15 '05 #26
"100" <10*@100.com> wrote in message
news:uh**************@tk2msftngp13.phx.gbl...
Some of the flags might have more the one bit set to 1 and thay might be not power of 2. This doesn't makes them *less flags* than the others, though.


Right. So the only sure way is to explicitly set the value, which is a good
practice anyway. I don't know how the compiler could be psychic and know
what the author intends. Somehow specifying a nonlinear increment sounds
like trouble in the long run.

-- Alan
Nov 15 '05 #27
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Thats my point, it should be. There is no reason to have it otherwise.
Logic dictates that it should be powers of 2, nothing else.


Using what you propose, what would be the value of b here?

[Flags]
enum Flags {
a = 6,
b
}
Nov 15 '05 #28
Nothing's wrong with the compiler. It behaves according to specs.

It would have been more accurate to say: "what's wrong with the C# language
.....".

IMO, the [Flags] attribute is not a very good design. The difference between
"normal" enums and "flags sets" is a rather fundamental one (from a semantic
viewpoint, not an implementation one) and it should be captured by a
language construct (i.e., a different declaration keyword) rather than by an
attribute. Then, it would be possible to have their numeric values be powers
of two by default. With the current design (the attribute hack), this would
be problematic because attributes are not supposed to influence things like
this (at least the way I understand them).

Bruno.

"Mr.Tickle" <Mr******@mrmen.com> a écrit dans le message de
news:ee**************@TK2MSFTNGP11.phx.gbl...
// Run this code and look for his obvious error by the compiler
namespace FlagCheck
{
using System;

[Flags]
enum SomeFlags
{
None = 0,
One,
Two,
Three,
Four,
Five,
Six,
All = One | Two | Three | Four | Five | Six
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
SomeFlags f;

f = SomeFlags.Four; // Set flag FOUR and ONLY FOUR, get ready for a
supprise

if ( (f & SomeFlags.One) > 0)
{
Console.WriteLine("flag one is set");
}

if ( (f & SomeFlags.Two) > 0)
{
Console.WriteLine("flag two is set");
}

if ( (f & SomeFlags.Three) > 0)
{
Console.WriteLine("flag three is set");
}

if ( (f & SomeFlags.Four) > 0)
{
Console.WriteLine("flag four is set");
}

if ( (f & SomeFlags.Five) > 0)
{
Console.WriteLine("flag five is set");
}

if ( (f & SomeFlags.Six) > 0)
{
Console.WriteLine("flag six is set");
}
Console.ReadLine();
}
}
}

Nov 15 '05 #29
if you need more than one bit set , override the default like i did in the
All = one | blah example. just as normal.

Have you ever seen a bit set to 3 by default? thats what it does today,
default behaviour on bitfields should be powers of 2, if you want otherwise
(like enums normally do) override it with a value, then it continues from
there.

"Alan Pretre" <no@spam> wrote in message
news:#3**************@TK2MSFTNGP12.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Thats my point, it should be. There is no reason to have it otherwise.
Logic dictates that it should be powers of 2, nothing else.


Using what you propose, what would be the value of b here?

[Flags]
enum Flags {
a = 6,
b
}

Nov 15 '05 #30
Normally when you have multiple bits set, you set them from the previous
one, like the "All" exmaple is an | of the other values. Youre argument by
setting it to 6 isnt valid, set it to the a = whatever the combination of
bits is which is individually named...
"Alan Pretre" <no@spam> wrote in message
news:#3**************@TK2MSFTNGP12.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Thats my point, it should be. There is no reason to have it otherwise.
Logic dictates that it should be powers of 2, nothing else.


Using what you propose, what would be the value of b here?

[Flags]
enum Flags {
a = 6,
b
}

Nov 15 '05 #31
we are talking DEFAULT assignments to fields that arnt overriden in value,
not forced onto all values. go figure youre argument, you have none.


"Alan Pretre" <no@spam> wrote in message
news:#3**************@TK2MSFTNGP12.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Thats my point, it should be. There is no reason to have it otherwise.
Logic dictates that it should be powers of 2, nothing else.


Using what you propose, what would be the value of b here?

[Flags]
enum Flags {
a = 6,
b
}

Nov 15 '05 #32
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
if you need more than one bit set , override the default like i did in the
All = one | blah example. just as normal.
That's right, you have to explictly set it. You're arguing about what C#
should do when the enum members are explicitly initialized.
Have you ever seen a bit set to 3 by default?
Whether I have or not is irrelevant. The question is, is it possible? The
answer is yes. In the general case bitfield constants can't be constrained
to single 1-bits only.
default behaviour on bitfields should be powers of 2, if you want otherwise (like enums normally do) override it with a value, then it continues from
there.


"Then it continues from there". How? There is no increment that is
appropriate for all bitfield cases (not even +1 as you've shown, but neither
will 2^n), so explicitly setting the constants is really the only sure way.

-- Alan
Nov 15 '05 #33
"Alan Pretre" <no@spam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
That's right, you have to explictly set it. You're arguing about what C#
should do when the enum members are explicitly initialized.


Typo, I should have said,

That's right, you have to explictly set it. You're arguing about what C#
should do when the enum members **aren't** explicitly initialized.

-- Alan
Nov 15 '05 #34
No but im talking DEFAULT behaviour, jesus get it thru yer thick skull.
defaults are set to multi bits set . thats the issue.
"Alan Pretre" <no@spam> wrote in message
news:#p**************@TK2MSFTNGP10.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
if you need more than one bit set , override the default like i did in the All = one | blah example. just as normal.
That's right, you have to explictly set it. You're arguing about what C#
should do when the enum members are explicitly initialized.
Have you ever seen a bit set to 3 by default?


Whether I have or not is irrelevant. The question is, is it possible?

The answer is yes. In the general case bitfield constants can't be constrained to single 1-bits only.
default behaviour on bitfields should be powers of 2, if you want otherwise
(like enums normally do) override it with a value, then it continues from there.


"Then it continues from there". How? There is no increment that is
appropriate for all bitfield cases (not even +1 as you've shown, but

neither will 2^n), so explicitly setting the constants is really the only sure way.
-- Alan

Nov 15 '05 #35
Im not talking about CONSTRAINING them im discussing DEFAULT values for non
set items. Dumbfuck.
"Alan Pretre" <no@spam> wrote in message
news:#p**************@TK2MSFTNGP10.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
if you need more than one bit set , override the default like i did in the All = one | blah example. just as normal.
That's right, you have to explictly set it. You're arguing about what C#
should do when the enum members are explicitly initialized.
Have you ever seen a bit set to 3 by default?


Whether I have or not is irrelevant. The question is, is it possible?

The answer is yes. In the general case bitfield constants can't be constrained to single 1-bits only.
default behaviour on bitfields should be powers of 2, if you want otherwise
(like enums normally do) override it with a value, then it continues from there.


"Then it continues from there". How? There is no increment that is
appropriate for all bitfield cases (not even +1 as you've shown, but

neither will 2^n), so explicitly setting the constants is really the only sure way.
-- Alan

Nov 15 '05 #36
Well if we have to EXPLICITLY set it, then the compiler is introducing
errors by not enforcing hits EXPLICID setting.

If its not explicidly set then its setting BY DEFAULT multiple bits. Im
proposing that DEFAULT for UNASSIGNED values are in powers of 2, which would
then be single set bits for UNASSIGNED values.

"Alan Pretre" <no@spam> wrote in message
news:uH**************@TK2MSFTNGP12.phx.gbl...
"Alan Pretre" <no@spam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
That's right, you have to explictly set it. You're arguing about what C# should do when the enum members are explicitly initialized.


Typo, I should have said,

That's right, you have to explictly set it. You're arguing about what C#
should do when the enum members **aren't** explicitly initialized.

-- Alan

Nov 15 '05 #37
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:O$**************@TK2MSFTNGP12.phx.gbl...
Dumbfuck.


Dude you're messed up.

-- Alan
Nov 15 '05 #38
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:uw**************@TK2MSFTNGP10.phx.gbl...
If its not explicidly set then its setting BY DEFAULT multiple bits. Im
proposing that DEFAULT for UNASSIGNED values are in powers of 2, which would then be single set bits for UNASSIGNED values.


But the way enums get set by default depends on the member's value
immediately before. You haven't yet said how you would determine an
appropriate default value for a member when the preceding value is not an
exact power of two. Now I'm wondering, can you converse without
profanities? And what is your name anyway?

-- Alan
Nov 15 '05 #39
Mr. Tickle, you need to learn what a bit is.

All integers are represented as bits.

The number 3 sets two bits. The number 7, 3 bits.

There is nothing to say that this isn't the way you intended it to work,
unless you say this isn't the way you intended it to work. Now that you
know how it works, and that it works according to design, quit yer griping
and get on with life, or go work with Java, which doesn't have enumerations.

"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Thats my point, it should be. There is no reason to have it otherwise.

Logic dictates that it should be powers of 2, nothing else.
"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
In article <eK**************@TK2MSFTNGP10.phx.gbl>, Mr******@mrmen.com
says...
WRONG, its a Flag, should be powers of 2


Check the documentation for the FlagsAttribute class:

"Indicates that an enumeration can be treated as a bit field"

It says it "can be treated" as a bit field. It does not mean the
enumeration "will be defined" as a bitfield.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele


Nov 15 '05 #40
See how you are? No one wants to help you out. Your mannerisms alone make
you fail to acheive the one thing you want: To convince anyone at all that
you are right about this. The argumentative style is working against you.
The profanity makes you look less than moderately intelligent, and certainly
uncultured. Time to sign up for charm school.

"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:O$**************@TK2MSFTNGP12.phx.gbl...
Im not talking about CONSTRAINING them im discussing DEFAULT values for non set items. Dumbfuck.
"Alan Pretre" <no@spam> wrote in message
news:#p**************@TK2MSFTNGP10.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
if you need more than one bit set , override the default like i did in the All = one | blah example. just as normal.


That's right, you have to explictly set it. You're arguing about what C#
should do when the enum members are explicitly initialized.
Have you ever seen a bit set to 3 by default?


Whether I have or not is irrelevant. The question is, is it possible?

The
answer is yes. In the general case bitfield constants can't be

constrained
to single 1-bits only.
default behaviour on bitfields should be powers of 2, if you want

otherwise
(like enums normally do) override it with a value, then it continues

from there.


"Then it continues from there". How? There is no increment that is
appropriate for all bitfield cases (not even +1 as you've shown, but

neither
will 2^n), so explicitly setting the constants is really the only sure

way.

-- Alan


Nov 15 '05 #41
Have you actually thought enough inside the box to get anything done?

Tell ya what, why don't you specify a set of rules that will create the
default behavior you are looking for, and also handle the case if the
programmer specifies one of the values to be "b = 6"?

You keep making these arguments and stating how clear it is that this is the
way it should be. Everyone keeps coming back and saying that there is no
set of rules to handle that case, in general. Why don't you explain them?

"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Would it not be useful to be able specify an increment like powers of 2 for bitfields, or any other increment somehow? Why must it always be one, I know what the damnn manual says, dont quote me on that crap, I am thinking OUT OF THE BOX (something which alot of you lot have yet to experience)

"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
In article <eK**************@TK2MSFTNGP10.phx.gbl>, Mr******@mrmen.com
says...
WRONG, its a Flag, should be powers of 2


Check the documentation for the FlagsAttribute class:

"Indicates that an enumeration can be treated as a bit field"

It says it "can be treated" as a bit field. It does not mean the
enumeration "will be defined" as a bitfield.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele


Nov 15 '05 #42
Hi Alan,

I think it still would be handly if the compiler would issue a warning that
an enum marked as "[Flags]" has members that haven't been explicitly
initialized and they have been therefore assigned values that are not powers
of 2 due to the increment.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Alan Pretre" <no@spam> wrote in message
news:OL**************@tk2msftngp13.phx.gbl...
"Mr.Tickle" <Mr******@mrmen.com> wrote in message
news:uw**************@TK2MSFTNGP10.phx.gbl...
If its not explicidly set then its setting BY DEFAULT multiple bits. Im
proposing that DEFAULT for UNASSIGNED values are in powers of 2, which

would
then be single set bits for UNASSIGNED values.


But the way enums get set by default depends on the member's value
immediately before. You haven't yet said how you would determine an
appropriate default value for a member when the preceding value is not an
exact power of two. Now I'm wondering, can you converse without
profanities? And what is your name anyway?

-- Alan


Nov 15 '05 #43
"Mr.Tickle" <Mr******@mrmen.com> wrote in news:eeFm2tmkDHA.3688
@TK2MSFTNGP11.phx.gbl:
// Run this code and look for his obvious error by the compiler
namespace FlagCheck
{
using System;

[Flags]
enum SomeFlags
{
None = 0,
One,
Two,
Three,
Four,
Five,
Six,
All = One | Two | Three | Four | Five | Six
}


You assume the [Flags] attribute makes the values in the enum 1, 2,
4, 8, 16, 32 etc.. This is not always wanted.

Perhaps you have flags which run like this: 1, 2, 3 (1 OR 2), 4, 8
etc.. How would that be determined? In other words: you initial thoughts
of 'it should be a bitfield' are not true because that would be limiting.
However because you can use bitwise operators with enums no matter what,
the attribute is of no use in C# IMHO. Oh, and always write out your
flag's values when you want to use them in OR operations :)

FB
--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
Nov 15 '05 #44

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

Similar topics

43
by: Mr.Tickle | last post by:
// Run this code and look for his obvious error by the compiler namespace FlagCheck { using System; enum SomeFlags {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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,...
0
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,...
0
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...

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.