//------------------------------------------------------------------------------------ // NOTE: To compile this file from the command prompt: // // csc -r:System.Drawing.dll CSReadOnlyWriteOnlyProperties.cs // //------------------------------------------------------------------------------------ using System; // For Console class using System.Drawing; // For Color class using System.IO; // For file-handling classes // ----------------------------------------------------- // Define a class representing a football team // ----------------------------------------------------- namespace DemoMembers { public class FootballTeam { // Fields private string mName; // Name of team private Color mJerseyColor; // Color of jerseys private int mWins, mDraws, mDefeats; // Game stats private bool mLogging; // Log results to file? // Constructor. public FootballTeam(string name, Color jersey) { mName = name; mJerseyColor = jersey; } // Read-only property to get the team name. public string Name { get { return mName; } } // Read-only property to calculate points earned. public int Points { get { return (mWins * 3) + (mDraws * 1); } } // Read-write property to get and set jersey color public Color JerseyColor { get { return mJerseyColor; } set { if (value.Equals(Color.Black)) { throw new ArgumentException("Cannot have black jerseys"); } mJerseyColor = value; } } // Write-only property to enable/disable logging. public bool Logging { set { mLogging = value; } } // Private read-only property, to get FileStream for logging. private FileStream LogStream { get { try { return new FileStream(mName + ".log", FileMode.Append, FileAccess.Write); } catch (Exception) { return new FileStream("Default.log", FileMode.Append, FileAccess.Write); } } } // Method to enter the result of a game. public void PlayGame(string opponent, int goalsFor, int goalsAgainst) { // Increment the appropriate counter. if (goalsFor > goalsAgainst) mWins++; else if (goalsFor == goalsAgainst) mDraws++; else mDefeats++; // If logging is enabled, log result to file. if (mLogging) { StreamWriter writer = new StreamWriter(LogStream); writer.WriteLine("{0} {1}-{2} {3}", mName, goalsFor, goalsAgainst, opponent); writer.Flush(); writer.Close(); } } } // ----------------------------------------------------- // Client code to test the FootballTeam class. // ----------------------------------------------------- static class ReadOnlyWriteOnlyPropertiesDemo { public static void DoDemo() { Console.WriteLine("\nReadOnlyWriteOnlyPropertiesDemo"); Console.WriteLine("------------------------------------------------------"); // Create a FootballTeam object. FootballTeam myTeam = new FootballTeam("Swansea", Color.White); // Use Name read-only property. Console.WriteLine("Team: {0}", myTeam.Name); // Use JerseyColor read-write property. myTeam.JerseyColor = Color.Red; Console.WriteLine("Jerseys: {0}", myTeam.JerseyColor); // Use Logging write-only property. myTeam.Logging = true; // Play some games. myTeam.PlayGame("Liverpool", 1, 1); myTeam.PlayGame("Chelsea", 3, 1); myTeam.PlayGame("Manchester United", 0, 4); myTeam.PlayGame("Everton", 2, 0); // Use Points property to calculate points earned Console.WriteLine("Points: {0}", myTeam.Points); } } }