-1

This is the code I have at the moment:

enum values { one, two, three, four, five, six, seven, eight, nine, ten, eleven };
    private void button1_Click(object sender, EventArgs e)
    {
        values Ace = values.one;
        values Ace = values.eleven;
        values King = values.ten;
        values Queen = values.ten;
        values Jack = values.ten;
        values Ten = values.ten;
        values Nine = values.nine;

I wish to assign the Ace name to the Enumeration value of one and eleven. How can I do this?

Amicable
  • 3,115
  • 3
  • 49
  • 77
Mr. Bottell
  • 41
  • 2
  • 8
  • 2
    Why are you using enum values? This should probably be represented by some nice, clear, OO classes – David L Jun 11 '13 at 20:14
  • http://stackoverflow.com/questions/17052091/array-for-blackjack-cards-strings-or-integers-c-sharp-newbie see this question for the reason why I have chosen to use enums – Mr. Bottell Jun 11 '13 at 20:16
  • I place C# Newbie on my question for a reason, so people don't throw loads of new words and giving different ways and easier ways to do when I do not know how. – Mr. Bottell Jun 11 '13 at 20:28
  • Enums are a very powerful tool, but not used properly here because you have two values which are basically the same thing. Like David and Amicable suggest create a class to represent your data. – Jay Jun 11 '13 at 20:31
  • It seems in my previous question most ignored the C# newbie part too, that's why I'm at this stage – Mr. Bottell Jun 11 '13 at 20:41

4 Answers4

1

Use the bitwise or operator: |

values Ace = values.one | values.eleven;

However, you'll get very odd behavior unless you define your enum with flags.

[Flags]
enum values
{
    one = 0x1,
    two = 0x2,
    three = 0x4,
    four = 0x8,
    six = 0x01,
    seven = 0x02,
    eight = 0x04,
    nine = 0x08,
    ten = 0x001,
    eleven = 0x002
}

All that aside, if you are using the values enum for determining point value, and enum is not the correct structure. Try an int[] to store the point values.

Community
  • 1
  • 1
Ed Chapel
  • 6,842
  • 3
  • 30
  • 44
0

You need the Flags attribute for your Enum and use the or symbol on it |

[Flags]
enum values { one = 1 << 0, two = 1 << 1, three = 1 << 2, four = 1 << 3, five = 1 << 4, six = 1 << 5, seven = 1 << 6, eight = 1 << 7, nine = 1 << 8, ten = 1 << 9, eleven = 1 << 10 };

private void button1_Click(object sender, EventArgs e)
{
    values Ace = values.one | values.eleven;
    values King = values.ten;
    values Queen = values.ten;
    values Jack = values.ten;
    values Ten = values.ten;
    values Nine = values.nine;

You may need to modify some of your other code to handle the fact that values is now a Flags style enum (like how i had to define all the flag values).

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
0

You can use the bitwise or operator iif: Your enum has a Flags attribute:

[Flags]
enum values { one, two....}

or you declare value explicitly that are flagwise:

ie: enum{one=1,two=2,three=4,four=8,five=16 ...}

as you can see, by choosing subsequent power of two, you have each value with just one bit set:

1= 00000001
2= 00000010
4= 00000100
8= 00001000
16=00010000

with this strategy you can bitwise combine any flags, binary rep should help to figure out why. otherwise the bitwise or operator will wrongly overlap some values.

Pay attention that normal == operator will fail if you have a combined value and you need to check against a single one, you have to check with a mask:

if((value.one & yourvar)!=0)
        ....
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
-1
class Card
{
    public string Name { get; set; }
    public int Values[] { get; set; } // alternatively, an enum

    // Examples of how I would expand this for use in a game
    public String Suit { get; set; } // alternatively, an enum.
    public Boolean Dealt { get; set;}
}

I would use an OO approach such as this.

Amicable
  • 3,115
  • 3
  • 49
  • 77
  • This is overkill when you're simply wanting to provide enum values that are combinations of others. – DonBoitnott Jun 11 '13 at 20:18
  • 1
    I disagree, enum flags are dangerous, and it does not properly represent his domain. Using "real" .Net classes will future proof his solution as well as make it cleaner. – Jay Jun 11 '13 at 20:26
  • well, enums are used widely even in OOP. But in any case the solution you propose in my opinion does not add to much value respect to the enum, and you loose the intellisense and syntax checking. – Felice Pollano Jun 11 '13 at 20:33
  • I've expanded upon my example to demonstrate the extensibility, as this is the kind of use for a card representation I had in mind. An enum alone just seemed inflexible for actual use. – Amicable Jun 12 '13 at 09:57