[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

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

Mr.Tickle

10/14/2003 3:59:00 PM

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


14 Answers

Stephen Martin

10/14/2003 1:26:00 PM

0

Works fine for me.

The out put is:

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

as expected.


"Mr.Tickle" <MrTickle@mrmen.com> wrote in message
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
> }
>
> 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();
> }
> }
> }
>
>


Max Bolingbroke <batterseapower

10/14/2003 4:17:00 PM

0

"Mr.Tickle" <MrTickle@mrmen.com> wrote in message
news:eeFm2tmkDHA.3688@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


Mr.Tickle

10/14/2003 4:25:00 PM

0

It should be powers of 2 by default for flags.

Thats my point.


"Alan Pretre" <no@spam> wrote in message
news:#sbsc6mkDHA.4008@TK2MSFTNGP11.phx.gbl...
> "Mr.Tickle" <MrTickle@mrmen.com> wrote in message
> news:eeFm2tmkDHA.3688@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
>
>


Mr.Tickle

10/14/2003 4:27:00 PM

0

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" <MrTickle@mrmen.com> wrote in message
news:uPpMV8mkDHA.1740@TK2MSFTNGP12.phx.gbl...
> It should be powers of 2 by default for flags.
>
> Thats my point.
>
>
> "Alan Pretre" <no@spam> wrote in message
> news:#sbsc6mkDHA.4008@TK2MSFTNGP11.phx.gbl...
> > "Mr.Tickle" <MrTickle@mrmen.com> wrote in message
> > news:eeFm2tmkDHA.3688@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
> >
> >
>
>


John P

10/14/2003 5:18:00 PM

0

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" <MrTickle@mrmen.com> wrote in message
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
> }
>
> 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

10/14/2003 5:32:00 PM

0

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" <MrTickle@mrmen.com> wrote in message
> 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
>> }
>>
>> 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

Stoitcho Goutsev \(100\) [C# MVP]

10/14/2003 5:37:00 PM

0

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" <MrTickle@mrmen.com> wrote in message
news:uUN6Q9mkDHA.2676@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" <MrTickle@mrmen.com> wrote in message
> news:uPpMV8mkDHA.1740@TK2MSFTNGP12.phx.gbl...
> > It should be powers of 2 by default for flags.
> >
> > Thats my point.
> >
> >
> > "Alan Pretre" <no@spam> wrote in message
> > news:#sbsc6mkDHA.4008@TK2MSFTNGP11.phx.gbl...
> > > "Mr.Tickle" <MrTickle@mrmen.com> wrote in message
> > > news:eeFm2tmkDHA.3688@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
> > >
> > >
> >
> >
>
>


Fabian Schmied

10/14/2003 7:06:00 PM

0

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


Max Bolingbroke <batterseapower

10/14/2003 9:08:00 PM

0

"100" <100@100.com> wrote in message
news:uhQjHknkDHA.1656@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


Bruno Jouhier

10/14/2003 9:12:00 PM

0

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" <MrTickle@mrmen.com> a écrit dans le message de
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
> }
>
> 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();
> }
> }
> }
>
>