C# data structure for multiple unit conversions -


I have a C # app and give me 3 different units (for example: liters, gallons, and pints) Need to convert between. .

The app needs to know about some sections of liquid, say: 1 pin, 10 pin, 20 pin and 100 pins. I intend to make calculations and hard code values ​​(not ideal but necessary),

I'm looking for a data structure that will allow me to easily convert from one unit to another.

Any suggestions?

Please note: I'm not actually using the versions of liquid, an example of this!

You can store a matrix of conversion factors where

  • A: What liters
  • b: pint's
  • c: are gallons

you want (not accurate, but assume that one Liter has two pins and 4 liters in gallon)

  abca 1 2 0.25 b 0.5 1 0.125 c 4 8 1  

Alternatively, you can There is another type that can decide that everything gets converted to a basic value (liters) before it gets converted, Programmed you just need the first line.

Wrap this method in a method that takes several units and "from" type and "two" type for conversion.

Edit: Some codes, as requested

  public enum volume type { Lit. = 0, pint = 1, gallon = 2} public static double-convert unit (at units, volume type, volume type), double [], double double [] (1, 2, 0.25}, new double [] 4, 8, 1}}; return units * factor [from (int)] to [(int)];} public static void ShowConversion (int Old Unit, Volume Type, Volume Type), {Double New Units = Convert Units ( From the Urana units, from) Console.light line ("{0} {1} = {2} {3}", from old units, to.tring (), new units, brokenstrings ());} static zero main (String [] args) (pronouns (1, volume type, litre, volume type.); // = 1 ShowConversion (1, volume.Tip.Liter, VolumeTip. Pin); // = 2 ShowConversion (1, VolumeTip.Liter, Volume Type .gallon); // = 4 ShowConversion (1, volume type, pin, volume type); // = 1 ShowConversion (1, volume type, pin, volumetric P. Litter); // = 0.5 ShowConversion (1, Volume Type. Pin, Volume Type Gallon); // = 0.125 ShowConversion (1, volume type. Golan, volume type. Golan); // = 1 Show conversion (1, volume type., Golan, volume type); // = 8 ShowConversion (1, VolumeType Gallon, VolumeTip.Litter); // = 4 ShowConversion (10, VolumeTip.Litter, VolumeTipPin); // = 20 ShowConversion (20, volume type gallon, volume type); // = 160}  

Comments