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

S

Stephen Martin

Works fine for me.

The out put is:

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

as expected.
 
M

Mr.Tickle

// 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();
}
}
}
 
A

Alan Pretre

Mr.Tickle said:
// 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
 
A

Alan Pretre

Mr.Tickle said:
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
 
M

Mr.Tickle

WRONG, its a Flag, should be powers of 2. you ninney


You expected wrong.


Stephen Martin said:
Works fine for me.

The out put is:

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

as expected.


Mr.Tickle said:
// 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();
}
}
}
 
M

Mr.Tickle

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)?
 
M

Mr.Tickle

They influence the operators , why not the enum default values.

So in a way they do MORPH it. fagot.ur a hoot.
 
P

Patrick Steele [MVP]

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.
 
A

Alan Pretre

Mr.Tickle said:
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
 
M

Mr.Tickle

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.
 
M

Mr.Tickle

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 said:
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.
 
M

Mr.Tickle

Sure, RTM RTM blah, suppose you say "Linux owns j000, go rtfm l0ser!"

u sad ****k. 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.
 
M

Mr.Tickle

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.
 
A

Alan Pretre

Mr.Tickle said:
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
 
J

Jon Skeet [C# MVP]

Alan Pretre said:
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#.
 
M

Mr.Tickle

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)
 
A

Alan Pretre

Mr.Tickle said:
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
 
M

Mr.Tickle

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.
 
M

Mr.Tickle

If you cant see the application of specifying incrementts in enumerations,
you need glasses.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top