Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 15th, 2011, 12:08 PM
Authorized User
Points: 149, Level: 2
Points: 149, Level: 2 Points: 149, Level: 2 Points: 149, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Location: Belgrade,Serbia
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default Chapter 11 page 290 Decks.cs

Code:
namespace Ch10CardLib
{
    public class Deck
    {
        private Card cards=new Card();
        public Deck()
        {
            
            for (int suitval = 0; suitval < 4; suitval++)
            {
                for (int rankval = 1; rankval < 14; rankval++)
                    cards.add(new Card((Suit)suitval,(Rank)rankval));            }
        }
cards.add(new Card((Suit)suitval,(Rank)rankval));

Why casting is used (Suit) (Rank)-casting from int (suitval,rankval) to string (enum Suit,Rank)?

Last edited by dampyr; November 15th, 2011 at 12:24 PM..
 
Old November 15th, 2011, 11:09 PM
Authorized User
Points: 149, Level: 2
Points: 149, Level: 2 Points: 149, Level: 2 Points: 149, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Location: Belgrade,Serbia
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default Chapter 11 Deep copy,page 302

Deck deck1=new Deck();
Deck deck2=(Deck)deck1.Clone()

Same question as previous one:why cast is used?

Last edited by dampyr; November 15th, 2011 at 11:10 PM.. Reason: Correction
 
Old November 19th, 2011, 03:56 PM
Wrox Author
Points: 1,163, Level: 13
Points: 1,163, Level: 13 Points: 1,163, Level: 13 Points: 1,163, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2010
Location: Austria
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Quote:
Originally Posted by dampyr View Post

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.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old November 20th, 2011, 06:27 AM
Authorized User
Points: 149, Level: 2
Points: 149, Level: 2 Points: 149, Level: 2 Points: 149, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Location: Belgrade,Serbia
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default

Thank You very much,Mr Nagel,Your explanations are very helpfull and concize.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 1, page 11/12 jonzi_456 BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 7 April 21st, 2010 09:18 PM
Chapter 11 Page 354 skijor BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 6 August 9th, 2009 08:02 AM
Chapter 1, page 11 typo? tumana BOOK Beginning Linux Programming, 3rd Edition 4 August 25th, 2006 10:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.