Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'          playing-card/playing-card-to-string/PlayingCard.cs - The struct Card that implements IFormattable.Lecture 8 - slide 17 : 37
Program 2

using System;

public enum CardSuite:byte 
          {Spades, Hearts, Clubs, Diamonds };
public enum CardValue: byte 
          {Ace = 1, Two = 2, Three = 3, Four = 4, Five = 5, 
           Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10, 
           Jack = 11, Queen = 12, King = 13};

public struct Card: IFormattable{
  private CardSuite suite;
  private CardValue value;

  public Card(CardSuite suite, CardValue value){
   this.suite = suite;
   this.value = value;
  }
 
  // Card methods and properties here... 
 
  public System.Drawing.Color Color (){
   System.Drawing.Color result;
   if (suite == CardSuite.Spades || suite == CardSuite.Clubs)
     result = System.Drawing.Color.Black;
   else
     result = System.Drawing.Color.Red;
   return result;
  }

  public override String ToString(){
    return this.ToString(null, null);
  }
 
  public String ToString(string format, IFormatProvider fp){
    if (format == null || format == "G" || format == "L") 
        return String.Format("Card Suite: {0}, Value: {1}, Color: {2}", 
                              suite, value, Color().ToString());

    else if (format == "S") 
        return String.Format("Card {0}: {1}", suite, (int)value);

    else if (format == "V") 
        return String.Format("Card value: {0}", value);

    else throw new FormatException(
                     String.Format("Invalid format: {0}", format));
  }   

}