Quote:
Originally Posted by dampyr
cards.add(new Card((Suit)suitval,(Rank)rankval));
Why casting is used (Suit) (Rank)-casting from int (suitval,rankval) to string (enum Suit,Rank)?
|
For the same reason a cast is done on page 105,
directionByte = (byte)myDirection;
Starting on page 102 you can read about enumerations. The enumeration has an underlying number type. However, assigning the enumeration values is type-safe.
On page 268 you can find the Card constructor. This constructor requires parameters of type Suit and Rank which are enum types. These enum types are defined on page 267.
Now you can create an Card instance this way:
Card c1 = new Card(Suit.Club, Rank.Ace);
Card c2 = new Card(Suit.Club, Rank.Deuce);
//...
Instead of passing Suit.Club and Rank.Ace, the underlying values (0 and 1), or for Suit.Club and Rank.Deuce 0 and 2 can be passed. However, the compiler doesn't allow passing int values to enum types without casting. With the cast you give the information to the compiler that it should ignore the issue and you are paying attention for yourself that you pass numbers that are in the valid range for the enum types.