Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I create a generic converter for units of measurement in C#?
    primarykey
    data
    text
    <p>I have been trying to learn a bit more about delegates and lambdas while working on a small cooking project that involves temperature conversion as well as some cooking measurement conversions such as Imperial to Metric and I've been trying to think of a way to make an extensible Unit converter.</p> <p>Here is what I started with, along with code comments on what some of my plans were. I have no plan to use it like the below, I was just testing out some features of C# I don't know very well, I am also unsure how to take this further. Does anyone have any suggestions on how to create what I am talking about in the comments below? Thanks</p> <pre><code>namespace TemperatureConverter { class Program { static void Main(string[] args) { // Fahrenheit to Celsius : [°C] = ([°F] − 32) × 5⁄9 var CelsiusResult = Converter.Convert(11M,Converter.FahrenheitToCelsius); // Celsius to Fahrenheit : [°F] = [°C] × 9⁄5 + 32 var FahrenheitResult = Converter.Convert(11M, Converter.CelsiusToFahrenheit); Console.WriteLine("Fahrenheit to Celsius : " + CelsiusResult); Console.WriteLine("Celsius to Fahrenheit : " + FahrenheitResult); Console.ReadLine(); // If I wanted to add another unit of temperature i.e. Kelvin // then I would need calculations for Kelvin to Celsius, Celsius to Kelvin, Kelvin to Fahrenheit, Fahrenheit to Kelvin // Celsius to Kelvin : [K] = [°C] + 273.15 // Kelvin to Celsius : [°C] = [K] − 273.15 // Fahrenheit to Kelvin : [K] = ([°F] + 459.67) × 5⁄9 // Kelvin to Fahrenheit : [°F] = [K] × 9⁄5 − 459.67 // The plan is to have the converters with a single purpose to convert to //one particular unit type e.g. Celsius and create separate unit converters //that contain a list of calculations that take one specified unit type and then convert to their particular unit type, in this example its Celsius. } } // at the moment this is a static class but I am looking to turn this into an interface or abstract class // so that whatever implements this interface would be supplied with a list of generic deligate conversions // that it can invoke and you can extend by adding more when required. public static class Converter { public static Func&lt;decimal, decimal&gt; CelsiusToFahrenheit = x =&gt; (x * (9M / 5M)) + 32M; public static Func&lt;decimal, decimal&gt; FahrenheitToCelsius = x =&gt; (x - 32M) * (5M / 9M); public static decimal Convert(decimal valueToConvert, Func&lt;decimal, decimal&gt; conversion) { return conversion.Invoke(valueToConvert); } } } </code></pre> <p><strong>Update:</strong> Trying to clarify my question:</p> <p>Using just my temperature example below, how would I create a class that contains a list of lambda conversions to Celsius which you then pass it a given temperature and it will try and convert that to Celsius (if the calculation is available)</p> <p>Example pseudo code:</p> <pre><code>enum Temperature { Celcius, Fahrenheit, Kelvin } UnitConverter CelsiusConverter = new UnitConverter(Temperature.Celsius); CelsiusConverter.AddCalc("FahrenheitToCelsius", lambda here); CelsiusConverter.Convert(Temperature.Fahrenheit, 11); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload