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

  • Thread starter Thread starter Stephen Martin
  • Start date Start date
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.
 
// 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();
}
}
}
 
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
 
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
 
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();
}
}
}
 
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)?
 
They influence the operators , why not the enum default values.

So in a way they do MORPH it. fagot.ur a hoot.
 
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.
 
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
 
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.
 
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.
 
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.
 
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 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
 
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#.
 
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)
 
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
 
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.
 
If you cant see the application of specifying incrementts in enumerations,
you need glasses.
 
Back
Top