Skip to content

Commit e2720b0

Browse files
authored
README: Update information referring to QuantityType removed in v5 (#1299)
Fixes #1297
1 parent 987e83d commit e2720b0

File tree

1 file changed

+53
-25
lines changed

1 file changed

+53
-25
lines changed

README.md

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -130,32 +130,57 @@ There are a handful of classes to help with this:
130130
- [UnitConverter](UnitsNet/UnitConverter.cs) for converting values to a different unit, with only strings or enum values
131131
- [UnitParser](UnitsNet/CustomCode/UnitParser.cs) for parsing unit abbreviation strings, such as `"cm"` to `LengthUnit.Centimeter`
132132

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+
135136
```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;
139143

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);
141146
```
142147

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.
145150
This is useful for populating lists of quantities and units for the user to choose.
146151

147152
```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, ...}
159184
```
160185

161186
#### Construct quantity
@@ -297,25 +322,28 @@ HowMuch q = quantityParser.Parse<HowMuch, HowMuchUnit>(
297322
![image](https://user-images.githubusercontent.com/787816/34920961-9b697004-f97b-11e7-9e9a-51ff7142969b.png)
298323
299324
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`.
301326

302327
#### 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"`.
304329

305330
```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();
307335
```
308336

309337
#### Update unit lists when selecting new quantity
310338
So user can only choose from/to units compatible with the quantity type.
311339

312340
```c#
313-
QuantityInfo quantityInfo = Quantity.GetInfo(quantityType);
341+
QuantityInfo quantityInfo = Quantity.ByName[quantityName];
314342

315343
_units.Clear();
316-
foreach (Enum unitValue in quantityInfo.Units)
344+
foreach (Enum unitValue in quantityInfo.UnitInfos.Select(ui => ui.Value))
317345
{
318-
_units.Add(unitValue);
346+
_units.Add(new UnitListItem(unitValue));
319347
}
320348
```
321349

0 commit comments

Comments
 (0)