Enumeration

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I need an enumeration that will have the following items:

A, B, C, AandB, AandC, BandC ...

Is there a better way to create such an enumeration or something
similar without specifying all the items?

Thanks,
Miguel
 
I need an enumeration that will have the following items:

A, B, C, AandB, AandC, BandC ...

Is there a better way to create such an enumeration or something
similar without specifying all the items?

You can't declare an enum without specifying all the items.

If you simply want a collection (i.e. not an actual enum type) giving all
possible pair-wise combinations of the collection, that's easy enough to
do algorithmically. But if that's what you're asking about, it's not at
all clear what you need help with. The basic idea is pretty
straightforward.

Pete
 
I need an enumeration that will have the following items:

A, B, C, AandB, AandC, BandC ...

Is there a better way to create such an enumeration or something
similar without specifying all the items?

Given x single values (like A, B, and C above), can the possible
"combination member" be ANY combination of those values from 2 to x? Like
AB, AC, BC, and ABC? Or is a combination limited to two "single" members? If
the first one, read up on the Flags attribute. If the second, well, I don't
see another way besides hand-crafting the combinations.
 
Peter Duniho skrev:
You can't declare an enum without specifying all the items.

If you simply want a collection (i.e. not an actual enum type) giving
all possible pair-wise combinations of the collection, that's easy
enough to do algorithmically. But if that's what you're asking about,
it's not at all clear what you need help with. The basic idea is pretty
straightforward.

You can declare and use enums as bitmasks
 
shapper said:
Hello,

I need an enumeration that will have the following items:

A, B, C, AandB, AandC, BandC ...

Is there a better way to create such an enumeration or something
similar without specifying all the items?

Thanks,
Miguel

Hello, Miguel.

Define your enum, as follows:

[Flags]
enum AnEnum
{
None = 0,
A = 1,
B = 2,
C = 4,
D = 8,
}

The 'Flags' attribute tells the runtime that the intention of the enum
is for combinations of it's members. You must identify each member of
the enum with a distinct value increasing in powers of two. The first
element, 'None' is not compulsory, but recommended.

Then, you can do this:

///
AnEnum AandB = AnEnum.A | AnEnum.B;
AnEnum BandCandD = AnEnum.B | AnEnum.C | AnEnum.D;
///

Armed with this knowledge, you can now test for the presence of a single
member like this:

///
if ((enumValue & AnEnum.A) == AnEnum.A) {
// A is present
}

if ((enumValue & AnEnum.B) == AnEnum.B) {
// B is present
}
///

Hopefully, this should get you going.

-- Tom
 
Back
Top