Enumartion flags

  • Thread starter Thread starter Jose Ines
  • Start date Start date
J

Jose Ines

Hello,
Can anyone give me a link where I can find a tutorial on how to make
enumaration flags? Not the structure (public enum MyFlags{...}) but how to
asign the members values (0x01, 0x02) etc...

Thanks.
Jose.
 
Jose,

If you are going to use flag values, then you have to make sure that the
values are zero and the powers of 2. So, your values would be:

0
1
2
4
8
16
32
64
128
etc
etc

Now when representing these with hex values, you will notice this:

0x0001
0x0002
0x0004
0x0008
0x0010
0x0020
0x0040
0x0080
0x0100
0x0200

Hope this helps.
 
Hi Jose,

[Flags]
enum MyFlags
{
First = 0x01,
Second = 0x02,
Third = 0x04
}

[Flags] attribute is not necessary but it changes the way ToString() method
works.

HTH
B\rgds
100
 
Hi Nicholas,

Flags doesn't have to be power of 2.

B\rgds
100

Nicholas Paldino said:
Jose,

If you are going to use flag values, then you have to make sure that the
values are zero and the powers of 2. So, your values would be:

0
1
2
4
8
16
32
64
128
etc
etc

Now when representing these with hex values, you will notice this:

0x0001
0x0002
0x0004
0x0008
0x0010
0x0020
0x0040
0x0080
0x0100
0x0200

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jose Ines said:
Hello,
Can anyone give me a link where I can find a tutorial on how to make
enumaration flags? Not the structure (public enum MyFlags{...}) but how to
asign the members values (0x01, 0x02) etc...

Thanks.
Jose.
 
Thanks Nicholas,
Do you know how to make a member represent several of the other
members?

public enum Border{
None = 0x0000,
Top = 0x0001,
Right = 0x0002
Left = 0x0004,
Bottom = 0x0008
All = ?
}

Thanks Again.
Jose.

Nicholas Paldino said:
Jose,

If you are going to use flag values, then you have to make sure that the
values are zero and the powers of 2. So, your values would be:

0
1
2
4
8
16
32
64
128
etc
etc

Now when representing these with hex values, you will notice this:

0x0001
0x0002
0x0004
0x0008
0x0010
0x0020
0x0040
0x0080
0x0100
0x0200

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jose Ines said:
Hello,
Can anyone give me a link where I can find a tutorial on how to make
enumaration flags? Not the structure (public enum MyFlags{...}) but how to
asign the members values (0x01, 0x02) etc...

Thanks.
Jose.
 
It doesn't have to be, but it should be if you are going to make it work
correctly.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

100 said:
Hi Nicholas,

Flags doesn't have to be power of 2.

B\rgds
100

message news:[email protected]...
Jose,

If you are going to use flag values, then you have to make sure that the
values are zero and the powers of 2. So, your values would be:

0
1
2
4
8
16
32
64
128
etc
etc

Now when representing these with hex values, you will notice this:

0x0001
0x0002
0x0004
0x0008
0x0010
0x0020
0x0040
0x0080
0x0100
0x0200

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jose Ines said:
Hello,
Can anyone give me a link where I can find a tutorial on how to make
enumaration flags? Not the structure (public enum MyFlags{...}) but
how
 
All = None | Top | Right | Left | Bottom

| sets a flag
& tests for it


Jose Ines said:
Thanks Nicholas,
Do you know how to make a member represent several of the other
members?

public enum Border{
None = 0x0000,
Top = 0x0001,
Right = 0x0002
Left = 0x0004,
Bottom = 0x0008
All = ?
}

Thanks Again.
Jose.

message news:[email protected]...
Jose,

If you are going to use flag values, then you have to make sure that the
values are zero and the powers of 2. So, your values would be:

0
1
2
4
8
16
32
64
128
etc
etc

Now when representing these with hex values, you will notice this:

0x0001
0x0002
0x0004
0x0008
0x0010
0x0020
0x0040
0x0080
0x0100
0x0200

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jose Ines said:
Hello,
Can anyone give me a link where I can find a tutorial on how to make
enumaration flags? Not the structure (public enum MyFlags{...}) but
how
 
Ok, in that case, it will work. However, this is not the common case,
but you are right. The only drawback to this is that you limit yourself to
the number of values that you can contain in your enumeration which can be
effective bit mappings (the max you can have is 32 right now).
 
Hi,
It doesn't have to be, but it should be if you are going to make it work
correctly.

What do you mean?
I may have flag that uses more then one bit.

for example one flag occupies the first two less significant bits (0 and
1st) (flag1 = 0x3)
then one flag that uses 3th and 5th bit let say (flag2 = 0x14).

enum MyFlags
{
Flag1 = 0x3,
Flag2 = 0x14
}


MyFlags flags = MyFlags.Flag1 | MyFlags.Flag2;

Do you want to say that this is incorrect or this won't work correctly?
Yes, I agree that usually one flag uses only one bit, but not always. There
is nothing wrong to have one flag represented by more then one bit. It will
work correctly as long as the semantic of those flags is correct.

B\rgds
100
 
When it goes for flags we are limited anyway by the underlying type of the
enumerations.
The ony reason for bringing this is because from the posts releated to
using enumerations as flags, I've seen, I got the impression that a lot of
programmers think that if they specify [Flags] attributes the values for the
enum's items *have to be* power of 2 otherwise they won't work.

B\rgds
100

Nicholas Paldino said:
Ok, in that case, it will work. However, this is not the common case,
but you are right. The only drawback to this is that you limit yourself to
the number of values that you can contain in your enumeration which can be
effective bit mappings (the max you can have is 32 right now).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

100 said:
Hi, it
work

What do you mean?
I may have flag that uses more then one bit.

for example one flag occupies the first two less significant bits (0 and
1st) (flag1 = 0x3)
then one flag that uses 3th and 5th bit let say (flag2 = 0x14).

enum MyFlags
{
Flag1 = 0x3,
Flag2 = 0x14
}


MyFlags flags = MyFlags.Flag1 | MyFlags.Flag2;

Do you want to say that this is incorrect or this won't work correctly?
Yes, I agree that usually one flag uses only one bit, but not always. There
is nothing wrong to have one flag represented by more then one bit. It will
work correctly as long as the semantic of those flags is correct.

B\rgds
100
 
Ok, in that case, it will work. However, this is not the common case,
but you are right. The only drawback to this is that you limit yourself to
the number of values that you can contain in your enumeration which can be
effective bit mappings (the max you can have is 32 right now).


Actually, you are limited to the type of your enum. By default, being an int,
that would be 32, but you can also just as easily define the following if you do
not need to spend 4 bytes of space. But then again, what is space. :)

public enum MyFlags : short
{
None = 0,
First = 1,
Second = 2,
Third = 4
}


.... and so on. In this case, the maximum effective bit mappings would be 16.

But then, I know that you knew this. I figured I would point out that the type
of an enum could be declared as well. I did not know this until last night.
 
Back
Top