@@ -130,32 +130,57 @@ There are a handful of classes to help with this:
130
130
- [ UnitConverter] ( UnitsNet/UnitConverter.cs ) for converting values to a different unit, with only strings or enum values
131
131
- [ UnitParser] ( UnitsNet/CustomCode/UnitParser.cs ) for parsing unit abbreviation strings, such as ` "cm" ` to ` LengthUnit.Centimeter `
132
132
133
- #### Enumerate quantities and units
134
- ` Quantity ` is the go-to class for looking up information about quantities at runtime.
133
+ #### Quantity - Enumerate quantities and units at runtime
134
+ Use ` Quantity ` class for looking up ` QuantityInfo ` and ` UnitInfo ` at runtime.
135
+
135
136
``` c#
136
- string [] Quantity .Names ; // ["Length", "Mass", ...]
137
- QuantityType [] Quantity .Types ; // [QuantityType.Length, QuantityType.Mass, ...]
138
- QuantityInfo [] Quantity .Infos ; // Information about all quantities and their units, types, values etc., see more below
137
+ string [] names = Quantity .Names ; // ["Length", "Mass", ...]
138
+ QuantityInfo [] qis = Quantity .Infos ; // All quantities and their units, types, values.
139
+
140
+ // Look up quantity by name.
141
+ QuantityInfo lengthInfo = Quantity .ByName [" Length" ];
142
+ UnitInfo [] lengthUnits = lengthInfo .UnitInfos ;
139
143
140
- QuantityInfo Quantity .GetInfo (QuantityType .Length ); // Get information about Length
144
+ // Look up unit by enum value (note: for extensibility, will instead look up by name in the future)
145
+ UnitInfo cmInfo = Quantity .GetUnitInfo (LengthUnit .Centimeter );
141
146
```
142
147
143
- #### Information about quantity type
144
- ` QuantityInfo ` makes it easy to enumerate names, units, types and values for the quantity type .
148
+ #### QuantityInfo - Information about a quantity
149
+ ` QuantityInfo ` makes it easy to get names, units, types and values for a quantity.
145
150
This is useful for populating lists of quantities and units for the user to choose.
146
151
147
152
``` c#
148
- QuantityInfo lengthInfo = Quantity .GetInfo (QuantityType .Length ); // You can get it statically here
149
- lengthInfo = Length .Info ; // or statically per quantity
150
- lengthInfo = Length .Zero .QuantityInfo ; // or dynamically from quantity instances
151
-
152
- lengthInfo .Name ; // "Length"
153
- lengthInfo .QuantityType ; // QuantityType.Length
154
- lengthInfo .UnitNames ; // ["Centimeter", "Meter", ...]
155
- lengthInfo .Units ; // [LengthUnit.Centimeter, LengthUnit.Meter, ...]
156
- lengthInfo .UnitType ; // typeof(LengthUnit)
157
- lengthInfo .ValueType ; // typeof(Length)
158
- lengthInfo .Zero ; // Length.Zero
153
+ // Different ways to look up the quantity info.
154
+ QuantityInfo lengthInfo = Quantity .ByName [" Length" ];
155
+ lengthInfo = Length .Info ;
156
+ lengthInfo = Length .FromMeters (1 ).QuantityInfo ;
157
+
158
+ // The quantity information.
159
+ lengthInfo .Name ; // "Length"
160
+ lengthInfo .UnitInfos ; // UnitInfo[] for its units Centimeter, Meter, etc.
161
+ lengthInfo .BaseUnitInfo ; // UnitInfo for LengthUnit.Meter
162
+ lengthInfo .BaseDimensions ; // {"Length": 1, "Mass": 0, ...}
163
+ lengthInfo .UnitType ; // typeof(LengthUnit)
164
+ lengthInfo .ValueType ; // typeof(Length)
165
+ lengthInfo .Zero ; // Length.Zero
166
+ ```
167
+
168
+ #### UnitInfo - Information about a unit
169
+ ` UnitInfo ` describes a unit, such as its enum value, names and its representation in SI base units.
170
+
171
+ ``` c#
172
+ // Different ways to look up the unit info.
173
+ var cm = Quantity .GetUnitInfo (LengthUnit .Centimeter );
174
+
175
+ if (Quantity .TryGetUnitInfo (LengthUnit .Centimeter , out UnitInfo tryGetCm )) {
176
+ cm = tryGetCm ;
177
+ }
178
+
179
+ // The unit information.
180
+ cm .Value ; // Enum value: LengthUnit.Centimeter
181
+ cm .Name ; // "Centimeter"
182
+ cm .PluralName ; // "Centimeters"
183
+ cm .BaseUnits ; // {"Length": Centimeter, "Mass": null, "Time": null, ...}
159
184
```
160
185
161
186
#### Construct quantity
@@ -297,25 +322,28 @@ HowMuch q = quantityParser.Parse<HowMuch, HowMuchUnit>(
297
322
! [image ](https :// user-images.githubusercontent.com/787816/34920961-9b697004-f97b-11e7-9e9a-51ff7142969b.png)
298
323
299
324
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 .
300
- The quantity list box contains ` QuantityType ` values such as `QuantityType . Length ` and the two unit list boxes contain `Enum ` values , such as `LengthUnit .Meter `.
325
+ The quantity list box contains quantity names , such as `" Length" `. The two unit list boxes contain `Enum ` values , such as `LengthUnit .Meter `.
301
326
302
327
#### Populate quantity selector
303
- Use `Quantity ` to enumerate all quantity type enum values , such as `QuantityType . Length ` and `QuantityType . Mass `.
328
+ Use `Quantity ` to enumerate all quantity names , such as `" Length" ` and `" Mass" `.
304
329
305
330
```c #
306
- this .Quantities = Quantity .Types ; // QuantityType[]
331
+ this .Quantities = Quantity .Names ; // string[]
332
+
333
+ // or
334
+ this .Quantities = Quantity .Infos .Select (i => i .Name ).ToList ();
307
335
```
308
336
309
337
#### Update unit lists when selecting new quantity
310
338
So user can only choose from / to units compatible with the quantity type .
311
339
312
340
```c #
313
- QuantityInfo quantityInfo = Quantity .GetInfo ( quantityType ) ;
341
+ QuantityInfo quantityInfo = Quantity .ByName [ quantityName ] ;
314
342
315
343
_units .Clear ();
316
- foreach (Enum unitValue in quantityInfo .Units )
344
+ foreach (Enum unitValue in quantityInfo .UnitInfos . Select ( ui => ui . Value ) )
317
345
{
318
- _units .Add (unitValue );
346
+ _units .Add (new UnitListItem ( unitValue ) );
319
347
}
320
348
```
321
349
0 commit comments