Skip to content

Commit ae3c904

Browse files
committed
README: Add pseudo code for converter app
1 parent 3c772af commit ae3c904

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,28 @@ For more examples on dynamic parsing and conversion, see the unit conversion app
150150
![image](https://user-images.githubusercontent.com/787816/34920961-9b697004-f97b-11e7-9e9a-51ff7142969b.png)
151151

152152

153-
This example shows how you can create a dynamic unit converter, where the user selects the quantity to convert, such as `Length` or `Mass`, then selects to convert from `Meter` to `Centimeter` and types in a value for how many meters.
153+
This example shows how you can create a dynamic unit converter, where the user selects the quantity to convert, such as `Temperature`, then selects to convert from `DegreeCelsius` to `DegreeFahrenheit` and types in a numeric value for how many degrees Celsius to convert.
154+
155+
Pseudo-code for converter app:
156+
```c#
157+
// Populate quantity selector ("Length", "Mass", "Force" etc)
158+
string[] quantityNames = UnitsHelper.QuantityNames;
159+
160+
string selectedQuantityName = "Temperature"; // Selected by user
161+
QuantityType selectedQuantity = Enum.Parse<QuantityType>(selectedQuantityName); // QuantityType.Temperature
162+
163+
// Populate from/to unit selectors when quantity selection changes
164+
string[] unitNames = UnitsHelper.GetUnitNamesForQuantity(selectedQuantity).ToArray();
165+
myGui.UpdateFromToListsOfUnits(unitNames);
166+
167+
// Assign these from GUI selection
168+
double fromValue = 25;
169+
string fromUnitName = "DegreeCelsius";
170+
string toUnitName = "DegreeFahrenheit";
171+
172+
// Convert using from value and selected quantity/unit names
173+
double convertedValue = UnitConverter.ConvertByName(fromValue, selectedQuantityName, fromUnitName, toUnitName);
174+
```
154175

155176
NOTE: There are still some limitations in the library that requires reflection to enumerate units for quantity and getting the abbreviation for a unit, when we want to dynamically enumerate and convert between units.
156177

0 commit comments

Comments
 (0)