Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Beginning Visual C# 2010
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old November 15th, 2011, 11:08 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
Send a message via Skype™ to dampyr
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 11:24 AM.
  #2 (permalink)  
Old November 15th, 2011, 10: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
Send a message via Skype™ to dampyr
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 10:10 PM. Reason: Correction
  #3 (permalink)  
Old November 19th, 2011, 02:56 PM
Wrox Author
Points: 1,160, Level: 13
Points: 1,160, Level: 13 Points: 1,160, Level: 13 Points: 1,160, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2010
Location: Austria
Posts: 174
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
  #4 (permalink)  
Old November 20th, 2011, 05: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
Send a message via Skype™ to dampyr
Default

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


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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



All times are GMT -4. The time now is 06:21 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.