EssentialCSharp icon indicating copy to clipboard operation
EssentialCSharp copied to clipboard

which 14.04 do we want?

Open BenjaminMichaelis opened this issue 3 years ago • 0 comments

@MarkMichaelis which 14.04 do we want? we use this one in the book currently:

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_04
{
    using System;

    public class Thermostat
    {
        // Define the event publisher
        public Action<float>? OnTemperatureChange { get; set; }

        public float CurrentTemperature
        {
            get { return _CurrentTemperature; }
            set
            {
                if(value != CurrentTemperature)
                {
                    _CurrentTemperature = value;

                    // Call subscribers
                    // Justification: Incomplete, check for null needed.
                    #pragma warning disable CS8602 // Dereference of a possibly null reference.
                    OnTemperatureChange(value);
                    #pragma warning restore CS8602 // Dereference of a possibly null reference.
                }
            }
        }
        private float _CurrentTemperature;
    }
}

but do we want to keep the other listing (we can as a placeholder or delete it?

Other listing is:

// TODO: Update listing in Manuscript
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_04
{
    using System;
    using Listing14_01;

    public class Program
    {
        public static void Main()
        {
            Thermostat thermostat = new Thermostat();
            Heater heater = new Heater(60);
            Cooler cooler = new Cooler(80);

            // Using C# 2.0 or later syntax
            thermostat.OnTemperatureChange +=
                heater.OnTemperatureChanged;
            thermostat.OnTemperatureChange +=
                cooler.OnTemperatureChanged;

            Console.Write("Enter temperature: ");
            string? temperature = Console.ReadLine();
            if (!int.TryParse(temperature, out int currentTemperature))
            {
                Console.WriteLine($"'{temperature}' is not a valid integer.");
                return;
            }
            thermostat.CurrentTemperature = currentTemperature;
        }
    }
}

BenjaminMichaelis avatar Jul 02 '22 02:07 BenjaminMichaelis