Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello Friends i am reading Microsoft Visual C# 2013 Step by Step by John Sharp to have my basics clear.

I have encountered a problem in understanding the following code in the book which says Use arrays to implement a card game.

1.Taking Two enumeration types.
C#
enum Value { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King,
Ace }

C#
enum Suit { Clubs, Diamonds, Hearts, Spades }

2.Creating PlayingCard class
C#
class PlayingCard
{
  private readonly Suit suit;
  private readonly Value value;
  
  public PlayingCard(Suit s, Value v)
  {
    this.suit = s;
    this.value = v;
  }
  /*public override string ToString()
  {
    
    return result;
  }*/
  
  public Suit CardSuit()
  {
    return this.suit;
  }
  
  public Value CardValue()
  {
    return this.value;
  }
}

In the above class i have commented part of the code which i am unable to understand it properly.

Thanks in advance.
Posted
Updated 24-Apr-15 0:45am
v2
Comments
Florian Braun 24-Apr-15 6:56am    
did you test that Method before commenting it out?
Mirza Mustafa Ali Baig 24-Apr-15 9:31am    
I haven't checked the code......First of all i am unable to get the code which is their

1 solution

The commented part is not complete.
It must be:

C#
public override string ToString()
{
    string result = string.Format("{0} of {1}", this.value, this.suit);

    return result;
}


You missed the first line.

The method ToString() is used to replace the standard ToString() method
that every object has in order to return a custom string.

Background:

Every class in C# is automatically derived from the object class.
ToString() is a method of the object class.
Please read this:
https://msdn.microsoft.com/en-us/library/system.object.tostring%28v=vs.110%29.aspx[^]

A class therefore automatically has a ToString() method.

When you override the ToString() method
you can return a custom string.
 
Share this answer
 
v3
Comments
Mirza Mustafa Ali Baig 24-Apr-15 9:34am    
Actually your right ..i had missed the code part which you have completed.
Your are describing ToString() method two times in your explanation part, Can you please explain me briefly with an good example.
TheRealSteveJudge 24-Apr-15 9:49am    
Please have a look at the updated solution.
Mirza Mustafa Ali Baig 24-Apr-15 9:55am    
Thank you @Steve Really appreciate your time and efforts.
Mirza Mustafa Ali Baig 24-Apr-15 12:48pm    
The Override Keyword is used to override the tostring() method to return the type name along with its value.
//I got the answer to my question by this statement what you have provided in the link.

Thanks a lot Brother.:-)
TheRealSteveJudge 26-Apr-15 3:29am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900