You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Improve working dynamically with units and quantities (#576)
* R#: Don't set namespace for CustomCode folder
* README: Add pseudo code for converter app
* Add QuantityInfo to IQuantity
* Add static and instance props for QuantityInfo
* Add tests
* Add features to make sample apps not have to use reflection
- Add As() and ToUnit() on IQuantity with Enum parameters
- Add UnitConverter.Convert() and .TryConvert() with Enum parameters
* Update sample apps to not use reflection
* Rename UnitsHelper to Quantity, add dynamic TryParse()
* Adding BaseDimensions to QuantityInfo
* Add TypeWrapper to help with reflection code
It was not a good idea to use extension methods with similar names as some of the targets.
A wrapper type avoids this problem.
* Adding BaseUnit to QuantityInfo. Adding GetQuantitiesWithBaseDimensions helper to Quantity class
* Adding Unit to IQuantity
@@ -29,7 +29,7 @@ See [Upgrading from 3.x to 4.x](https://github.com/angularsen/UnitsNet/wiki/Upgr
29
29
*[Statically typed quantities and units](#static-typing) to avoid mistakes and communicate intent
30
30
*[Operator overloads](#operator-overloads) for arithmetic on quantities
31
31
*[Parse and ToString()](#culture) supports cultures and localization
32
-
*[Dynamically parsing and converting](#dynamic-parsing) quantities and units
32
+
*[Dynamically parse and convert](#dynamic-parsing) quantities and units
33
33
*[Example: Creating a unit converter app](#example-app)
34
34
*[Example: WPF app using IValueConverter to parse quantities from input](#example-wpf-app-using-ivalueconverter-to-parse-quantities-from-input)
35
35
*[Precision and accuracy](#precision)
@@ -125,29 +125,105 @@ Unfortunately there is no built-in way to avoid this, either you need to ensure
125
125
Example:
126
126
`Length.Parse("1 pt")` throws `AmbiguousUnitParseException` with message `Cannot parse "pt" since it could be either of these: DtpPoint, PrinterPoint`.
127
127
128
-
### <aname="dynamic-parsing"></a>Dynamically Parsing and Converting Quantities
128
+
### <aname="dynamic-parsing"></a>Dynamically Parse Quantities and Convert to Units
129
129
Sometimes you need to work with quantities and units at runtime, such as parsing user input.
130
-
There are three classes to help with this:
131
-
-[UnitParser](UnitsNet/CustomCode/UnitParser.cs) for parsing unit abbreviation strings like `cm` to `LengthUnit.Centimeter`
132
-
-[UnitAbbreviationsCache](UnitsNet/CustomCode/UnitAbbreviationsCache.cs) for looking up unit abbreviations like `cm` given type `LengthUnit` and value `1` (`Centimeter`)
133
-
-[UnitConverter](UnitsNet/UnitConverter.cs) for converting values given a quantity name `Length`, a value `1` and from/to unit names `Centimeter` and `Meter`
130
+
131
+
There are a handful of classes to help with this:
132
+
133
+
-[Quantity](UnitsNet/CustomCode/Quantity.cs) for parsing and constructing quantities as well as looking up units, names and quantity information dynamically
134
+
-[UnitConverter](UnitsNet/UnitConverter.cs) for converting values to a different unit, with only strings or enum values
135
+
-[UnitParser](UnitsNet/CustomCode/UnitParser.cs) for parsing unit abbreviation strings, such as `"cm"` to `LengthUnit.Centimeter`
136
+
137
+
#### Enumerate quantities and units
138
+
`Quantity` is the go-to class for looking up information about quantities at runtime.
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.
235
+
Thisexampleshowshowyoucancreateadynamicunitconverter, wheretheuserselectsthequantitytoconvert, suchas `Temperature`, thenselectstoconvertfrom `DegreeCelsius` to `DegreeFahrenheit` andtypesinanumericvalueforhowmanydegreesCelsiustoconvert.
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.
238
+
#### Populate quantity selector
239
+
Use `Quantity` toenumerateallquantitytypeenumvalues, suchas `QuantityType.Length` and `QuantityType.Mass`.
162
240
163
-
### <aname="example-app-hardcoded"></a>Example: Creating a unit converter app with hard coded quantities
241
+
```c#
242
+
this.Quantities=Quantity.Types; // QuantityType[]
243
+
```
164
244
165
-
If you can live with hard coding what quantities to convert between, then the following code snippet shows you one way to go about it:
245
+
#### Update unit lists when selecting new quantity
0 commit comments