Skip to content

Commit 76baf97

Browse files
committed
Remove TryConvert(), rename Convert() to As()
These were originally copied from UnitConverter class. Does not make sense to TryConvert now that the big unit enum was divided into a separate enum for each unit class. Convert() also did not make much sense, as you don't really convert Length to meters, but rather get that length in a meter representation. * Update tests
1 parent 50b36ab commit 76baf97

30 files changed

+351
-698
lines changed

Src/UnitsNet/GeneratedCode/Angle.g.cs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -234,48 +234,29 @@ public override int GetHashCode()
234234
#endregion
235235

236236
#region Conversion
237-
237+
238238
/// <summary>
239-
/// Try to dynamically convert from Angle to <paramref name="toUnit"/>.
239+
/// Convert to the unit representation in <paramref name="asUnit"/>.
240240
/// </summary>
241241
/// <param name="toUnit">Compatible unit to convert to.</param>
242-
/// <param name="newValue">Value in new unit if successful, zero otherwise.</param>
243-
/// <returns>True if the two units were compatible and the conversion was successful.</returns>
244-
public bool TryConvert(AngleUnit toUnit, out double newValue)
242+
/// <returns>Value in new unit if successful, exception otherwise.</returns>
243+
/// <exception cref="NotImplementedException">If conversion was not successful.</exception>
244+
public double As(AngleUnit unit)
245245
{
246-
switch (toUnit)
246+
switch (unit)
247247
{
248248
case AngleUnit.Degree:
249-
newValue = Degrees;
250-
return true;
249+
return Degrees;
251250
case AngleUnit.Gradian:
252-
newValue = Gradians;
253-
return true;
251+
return Gradians;
254252
case AngleUnit.Radian:
255-
newValue = Radians;
256-
return true;
253+
return Radians;
257254

258255
default:
259-
newValue = 0;
260-
return false;
256+
throw new NotImplementedException("unit: " + unit);
261257
}
262258
}
263259

264-
/// <summary>
265-
/// Dynamically convert from Angle to <paramref name="toUnit"/>.
266-
/// </summary>
267-
/// <param name="toUnit">Compatible unit to convert to.</param>
268-
/// <returns>Value in new unit if successful, exception otherwise.</returns>
269-
/// <exception cref="NotImplementedException">If conversion was not successful.</exception>
270-
public double Convert(AngleUnit toUnit)
271-
{
272-
double newValue;
273-
if (!TryConvert(toUnit, out newValue))
274-
throw new NotImplementedException("toUnit: " + toUnit);
275-
276-
return newValue;
277-
}
278-
279260
#endregion
280261

281262
/// <summary>
@@ -300,7 +281,7 @@ public string ToString(AngleUnit unit, CultureInfo culture = null)
300281
public string ToString(AngleUnit unit, CultureInfo culture, string format, params object[] args)
301282
{
302283
string abbreviation = UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit);
303-
var finalArgs = new object[] {Convert(unit), abbreviation}
284+
var finalArgs = new object[] {As(unit), abbreviation}
304285
.Concat(args)
305286
.ToArray();
306287

Src/UnitsNet/GeneratedCode/Area.g.cs

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -354,66 +354,41 @@ public override int GetHashCode()
354354
#endregion
355355

356356
#region Conversion
357-
357+
358358
/// <summary>
359-
/// Try to dynamically convert from Area to <paramref name="toUnit"/>.
359+
/// Convert to the unit representation in <paramref name="asUnit"/>.
360360
/// </summary>
361361
/// <param name="toUnit">Compatible unit to convert to.</param>
362-
/// <param name="newValue">Value in new unit if successful, zero otherwise.</param>
363-
/// <returns>True if the two units were compatible and the conversion was successful.</returns>
364-
public bool TryConvert(AreaUnit toUnit, out double newValue)
362+
/// <returns>Value in new unit if successful, exception otherwise.</returns>
363+
/// <exception cref="NotImplementedException">If conversion was not successful.</exception>
364+
public double As(AreaUnit unit)
365365
{
366-
switch (toUnit)
366+
switch (unit)
367367
{
368368
case AreaUnit.SquareCentimeter:
369-
newValue = SquareCentimeters;
370-
return true;
369+
return SquareCentimeters;
371370
case AreaUnit.SquareDecimeter:
372-
newValue = SquareDecimeters;
373-
return true;
371+
return SquareDecimeters;
374372
case AreaUnit.SquareFoot:
375-
newValue = SquareFeet;
376-
return true;
373+
return SquareFeet;
377374
case AreaUnit.SquareInch:
378-
newValue = SquareInches;
379-
return true;
375+
return SquareInches;
380376
case AreaUnit.SquareKilometer:
381-
newValue = SquareKilometers;
382-
return true;
377+
return SquareKilometers;
383378
case AreaUnit.SquareMeter:
384-
newValue = SquareMeters;
385-
return true;
379+
return SquareMeters;
386380
case AreaUnit.SquareMile:
387-
newValue = SquareMiles;
388-
return true;
381+
return SquareMiles;
389382
case AreaUnit.SquareMillimeter:
390-
newValue = SquareMillimeters;
391-
return true;
383+
return SquareMillimeters;
392384
case AreaUnit.SquareYard:
393-
newValue = SquareYards;
394-
return true;
385+
return SquareYards;
395386

396387
default:
397-
newValue = 0;
398-
return false;
388+
throw new NotImplementedException("unit: " + unit);
399389
}
400390
}
401391

402-
/// <summary>
403-
/// Dynamically convert from Area to <paramref name="toUnit"/>.
404-
/// </summary>
405-
/// <param name="toUnit">Compatible unit to convert to.</param>
406-
/// <returns>Value in new unit if successful, exception otherwise.</returns>
407-
/// <exception cref="NotImplementedException">If conversion was not successful.</exception>
408-
public double Convert(AreaUnit toUnit)
409-
{
410-
double newValue;
411-
if (!TryConvert(toUnit, out newValue))
412-
throw new NotImplementedException("toUnit: " + toUnit);
413-
414-
return newValue;
415-
}
416-
417392
#endregion
418393

419394
/// <summary>
@@ -438,7 +413,7 @@ public string ToString(AreaUnit unit, CultureInfo culture = null)
438413
public string ToString(AreaUnit unit, CultureInfo culture, string format, params object[] args)
439414
{
440415
string abbreviation = UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit);
441-
var finalArgs = new object[] {Convert(unit), abbreviation}
416+
var finalArgs = new object[] {As(unit), abbreviation}
442417
.Concat(args)
443418
.ToArray();
444419

Src/UnitsNet/GeneratedCode/Duration.g.cs

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -374,69 +374,43 @@ public override int GetHashCode()
374374
#endregion
375375

376376
#region Conversion
377-
377+
378378
/// <summary>
379-
/// Try to dynamically convert from Duration to <paramref name="toUnit"/>.
379+
/// Convert to the unit representation in <paramref name="asUnit"/>.
380380
/// </summary>
381381
/// <param name="toUnit">Compatible unit to convert to.</param>
382-
/// <param name="newValue">Value in new unit if successful, zero otherwise.</param>
383-
/// <returns>True if the two units were compatible and the conversion was successful.</returns>
384-
public bool TryConvert(DurationUnit toUnit, out double newValue)
382+
/// <returns>Value in new unit if successful, exception otherwise.</returns>
383+
/// <exception cref="NotImplementedException">If conversion was not successful.</exception>
384+
public double As(DurationUnit unit)
385385
{
386-
switch (toUnit)
386+
switch (unit)
387387
{
388388
case DurationUnit.Day:
389-
newValue = Days;
390-
return true;
389+
return Days;
391390
case DurationUnit.Hour:
392-
newValue = Hours;
393-
return true;
391+
return Hours;
394392
case DurationUnit.Microsecond:
395-
newValue = Microseconds;
396-
return true;
393+
return Microseconds;
397394
case DurationUnit.Millisecond:
398-
newValue = Milliseconds;
399-
return true;
395+
return Milliseconds;
400396
case DurationUnit.Minute:
401-
newValue = Minutes;
402-
return true;
397+
return Minutes;
403398
case DurationUnit.Month30Days:
404-
newValue = Month30Dayss;
405-
return true;
399+
return Month30Dayss;
406400
case DurationUnit.Nanosecond:
407-
newValue = Nanoseconds;
408-
return true;
401+
return Nanoseconds;
409402
case DurationUnit.Second:
410-
newValue = Seconds;
411-
return true;
403+
return Seconds;
412404
case DurationUnit.Week:
413-
newValue = Weeks;
414-
return true;
405+
return Weeks;
415406
case DurationUnit.Year365Days:
416-
newValue = Year365Dayss;
417-
return true;
407+
return Year365Dayss;
418408

419409
default:
420-
newValue = 0;
421-
return false;
410+
throw new NotImplementedException("unit: " + unit);
422411
}
423412
}
424413

425-
/// <summary>
426-
/// Dynamically convert from Duration to <paramref name="toUnit"/>.
427-
/// </summary>
428-
/// <param name="toUnit">Compatible unit to convert to.</param>
429-
/// <returns>Value in new unit if successful, exception otherwise.</returns>
430-
/// <exception cref="NotImplementedException">If conversion was not successful.</exception>
431-
public double Convert(DurationUnit toUnit)
432-
{
433-
double newValue;
434-
if (!TryConvert(toUnit, out newValue))
435-
throw new NotImplementedException("toUnit: " + toUnit);
436-
437-
return newValue;
438-
}
439-
440414
#endregion
441415

442416
/// <summary>
@@ -461,7 +435,7 @@ public string ToString(DurationUnit unit, CultureInfo culture = null)
461435
public string ToString(DurationUnit unit, CultureInfo culture, string format, params object[] args)
462436
{
463437
string abbreviation = UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit);
464-
var finalArgs = new object[] {Convert(unit), abbreviation}
438+
var finalArgs = new object[] {As(unit), abbreviation}
465439
.Concat(args)
466440
.ToArray();
467441

Src/UnitsNet/GeneratedCode/ElectricPotential.g.cs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -194,42 +194,25 @@ public override int GetHashCode()
194194
#endregion
195195

196196
#region Conversion
197-
197+
198198
/// <summary>
199-
/// Try to dynamically convert from ElectricPotential to <paramref name="toUnit"/>.
199+
/// Convert to the unit representation in <paramref name="asUnit"/>.
200200
/// </summary>
201201
/// <param name="toUnit">Compatible unit to convert to.</param>
202-
/// <param name="newValue">Value in new unit if successful, zero otherwise.</param>
203-
/// <returns>True if the two units were compatible and the conversion was successful.</returns>
204-
public bool TryConvert(ElectricPotentialUnit toUnit, out double newValue)
202+
/// <returns>Value in new unit if successful, exception otherwise.</returns>
203+
/// <exception cref="NotImplementedException">If conversion was not successful.</exception>
204+
public double As(ElectricPotentialUnit unit)
205205
{
206-
switch (toUnit)
206+
switch (unit)
207207
{
208208
case ElectricPotentialUnit.Volt:
209-
newValue = Volts;
210-
return true;
209+
return Volts;
211210

212211
default:
213-
newValue = 0;
214-
return false;
212+
throw new NotImplementedException("unit: " + unit);
215213
}
216214
}
217215

218-
/// <summary>
219-
/// Dynamically convert from ElectricPotential to <paramref name="toUnit"/>.
220-
/// </summary>
221-
/// <param name="toUnit">Compatible unit to convert to.</param>
222-
/// <returns>Value in new unit if successful, exception otherwise.</returns>
223-
/// <exception cref="NotImplementedException">If conversion was not successful.</exception>
224-
public double Convert(ElectricPotentialUnit toUnit)
225-
{
226-
double newValue;
227-
if (!TryConvert(toUnit, out newValue))
228-
throw new NotImplementedException("toUnit: " + toUnit);
229-
230-
return newValue;
231-
}
232-
233216
#endregion
234217

235218
/// <summary>
@@ -254,7 +237,7 @@ public string ToString(ElectricPotentialUnit unit, CultureInfo culture = null)
254237
public string ToString(ElectricPotentialUnit unit, CultureInfo culture, string format, params object[] args)
255238
{
256239
string abbreviation = UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit);
257-
var finalArgs = new object[] {Convert(unit), abbreviation}
240+
var finalArgs = new object[] {As(unit), abbreviation}
258241
.Concat(args)
259242
.ToArray();
260243

Src/UnitsNet/GeneratedCode/Flow.g.cs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -214,45 +214,27 @@ public override int GetHashCode()
214214
#endregion
215215

216216
#region Conversion
217-
217+
218218
/// <summary>
219-
/// Try to dynamically convert from Flow to <paramref name="toUnit"/>.
219+
/// Convert to the unit representation in <paramref name="asUnit"/>.
220220
/// </summary>
221221
/// <param name="toUnit">Compatible unit to convert to.</param>
222-
/// <param name="newValue">Value in new unit if successful, zero otherwise.</param>
223-
/// <returns>True if the two units were compatible and the conversion was successful.</returns>
224-
public bool TryConvert(FlowUnit toUnit, out double newValue)
222+
/// <returns>Value in new unit if successful, exception otherwise.</returns>
223+
/// <exception cref="NotImplementedException">If conversion was not successful.</exception>
224+
public double As(FlowUnit unit)
225225
{
226-
switch (toUnit)
226+
switch (unit)
227227
{
228228
case FlowUnit.CubicMeterPerHour:
229-
newValue = CubicMetersPerHour;
230-
return true;
229+
return CubicMetersPerHour;
231230
case FlowUnit.CubicMeterPerSecond:
232-
newValue = CubicMetersPerSecond;
233-
return true;
231+
return CubicMetersPerSecond;
234232

235233
default:
236-
newValue = 0;
237-
return false;
234+
throw new NotImplementedException("unit: " + unit);
238235
}
239236
}
240237

241-
/// <summary>
242-
/// Dynamically convert from Flow to <paramref name="toUnit"/>.
243-
/// </summary>
244-
/// <param name="toUnit">Compatible unit to convert to.</param>
245-
/// <returns>Value in new unit if successful, exception otherwise.</returns>
246-
/// <exception cref="NotImplementedException">If conversion was not successful.</exception>
247-
public double Convert(FlowUnit toUnit)
248-
{
249-
double newValue;
250-
if (!TryConvert(toUnit, out newValue))
251-
throw new NotImplementedException("toUnit: " + toUnit);
252-
253-
return newValue;
254-
}
255-
256238
#endregion
257239

258240
/// <summary>
@@ -277,7 +259,7 @@ public string ToString(FlowUnit unit, CultureInfo culture = null)
277259
public string ToString(FlowUnit unit, CultureInfo culture, string format, params object[] args)
278260
{
279261
string abbreviation = UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit);
280-
var finalArgs = new object[] {Convert(unit), abbreviation}
262+
var finalArgs = new object[] {As(unit), abbreviation}
281263
.Concat(args)
282264
.ToArray();
283265

0 commit comments

Comments
 (0)