Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class UnitConvert
- {
- private enum UnitsValue
- {
- mm = 1,
- cm = 10,
- dm = 100,
- m = 1000,
- km = 1000000
- }
- private Dictionary<string, int> UnitsDictionary = new Dictionary<string, int>();
- public UnitConvert()
- {
- foreach (var item in Enum.GetValues(typeof(UnitsValue)))
- {
- UnitsDictionary.Add(item.ToString(), (int)item);
- }
- }
- public UnitAndDistance ConvertAnUnitDistance(UnitAndDistance entryDistance, string toUnit)
- {
- if (UnitsDictionary.Any(x => x.Key == toUnit))
- {
- double convertedValue = (entryDistance.Value * UnitsDictionary
- .FirstOrDefault(x => x.Key == entryDistance.Unit)
- .Value) / UnitsDictionary.FirstOrDefault(x => x.Key == toUnit).Value;
- return new UnitAndDistance(toUnit, convertedValue);
- }
- else
- {
- throw new IndexOutOfRangeException("Cannot find that unit!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement