Skip to content

Commit 1a73c5f

Browse files
committed
codegen: Use _value field
1 parent 7a2da03 commit 1a73c5f

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

UnitsNet/Scripts/Include-GenerateQuantitySourceCode.ps1

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,16 @@ namespace UnitsNet
100100
public partial struct $quantityName : IComparable, IComparable<$quantityName>
101101
#endif
102102
{
103+
private readonly $baseType _value;
104+
103105
/// <summary>
104106
/// The numeric value this quantity was constructed with.
105107
/// </summary>
106-
public $baseType Value { get; }
108+
#if WINDOWS_UWP
109+
public double Value => Convert.ToDouble(_value);
110+
#else
111+
public $baseType Value => _value;
112+
#endif
107113
108114
/// <summary>
109115
/// The unit this quantity was constructed with.
@@ -120,7 +126,7 @@ namespace UnitsNet
120126
[Obsolete("Use the constructor that takes a unit parameter. This constructor will be removed in a future version.")]
121127
public $quantityName(double $baseUnitPluralNameLower)
122128
{
123-
Value = $convertToBaseType($baseUnitPluralNameLower);
129+
_value = $convertToBaseType($baseUnitPluralNameLower);
124130
Unit = BaseUnit;
125131
}
126132
@@ -130,9 +136,14 @@ namespace UnitsNet
130136
/// <param name="numericValue">Numeric value.</param>
131137
/// <param name="unit">Unit representation.</param>
132138
/// <remarks>Value parameter cannot be named 'value' due to constraint when targeting Windows Runtime Component.</remarks>
133-
public $quantityName($baseType numericValue, $unitEnumName unit)
139+
#if WINDOWS_UWP
140+
private
141+
#else
142+
public
143+
#endif
144+
$quantityName($baseType numericValue, $unitEnumName unit)
134145
{
135-
Value = numericValue;
146+
_value = numericValue;
136147
Unit = unit;
137148
}
138149
@@ -357,7 +368,7 @@ namespace UnitsNet
357368
#endif
358369
int CompareTo($quantityName other)
359370
{
360-
return Value.CompareTo(other.AsBaseNumericType(Unit));
371+
return AsBaseUnit$baseUnitPluralName().CompareTo(other.AsBaseUnit$baseUnitPluralName());
361372
}
362373
363374
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx
@@ -402,7 +413,7 @@ namespace UnitsNet
402413
return false;
403414
}
404415
405-
return Value.Equals((($quantityName) obj).AsBaseNumericType(Unit));
416+
return AsBaseUnit$baseUnitPluralName().Equals((($quantityName) obj).AsBaseUnit$baseUnitPluralName());
406417
}
407418
408419
/// <summary>
@@ -415,7 +426,7 @@ namespace UnitsNet
415426
/// <returns>True if the difference between the two values is not greater than the specified max.</returns>
416427
public bool Equals($quantityName other, $quantityName maxError)
417428
{
418-
return Math.Abs(Value - other.AsBaseNumericType(Unit)) <= maxError.AsBaseNumericType(Unit);
429+
return Math.Abs(AsBaseUnit$baseUnitPluralName() - other.AsBaseUnit$baseUnitPluralName()) <= maxError.AsBaseUnit$baseUnitPluralName();
419430
}
420431
421432
public override int GetHashCode()
@@ -715,12 +726,12 @@ namespace UnitsNet
715726
/// <returns>The value in the base unit representation.</returns>
716727
private $baseType AsBaseUnit$baseUnitPluralName()
717728
{
718-
if (Unit == $unitEnumName.$baseUnitSingularName) { return Value; }
729+
if (Unit == $unitEnumName.$baseUnitSingularName) { return _value; }
719730
720731
switch (Unit)
721732
{
722733
"@; foreach ($unit in $units) {
723-
$func = $unit.FromUnitToBaseFunc.Replace("x", "Value");@"
734+
$func = $unit.FromUnitToBaseFunc.Replace("x", "_value");@"
724735
case $unitEnumName.$($unit.SingularName): return $func;
725736
"@; }@"
726737
default:

0 commit comments

Comments
 (0)