@@ -69,45 +69,70 @@ public sealed partial class MagneticFlux
69
69
public partial struct MagneticFlux : IComparable , IComparable < MagneticFlux >
70
70
#endif
71
71
{
72
+ private readonly double _value ;
73
+
74
+ /// <summary>
75
+ /// The numeric value this quantity was constructed with.
76
+ /// </summary>
77
+ #if WINDOWS_UWP
78
+ public double Value => Convert . ToDouble ( _value ) ;
79
+ #else
80
+ public double Value => _value ;
81
+ #endif
82
+
72
83
/// <summary>
73
- /// Base unit of MagneticFlux .
84
+ /// The unit this quantity was constructed with .
74
85
/// </summary>
75
- private readonly double _webers ;
86
+ public MagneticFluxUnit Unit { get ; }
76
87
77
88
// Windows Runtime Component requires a default constructor
78
89
#if WINDOWS_UWP
79
- public MagneticFlux ( ) : this ( 0 )
90
+ public MagneticFlux ( ) : this ( 0 , BaseUnit )
80
91
{
81
92
}
82
93
#endif
83
94
95
+ [ Obsolete ( "Use the constructor that takes a unit parameter. This constructor will be removed in a future version." ) ]
84
96
public MagneticFlux ( double webers )
85
97
{
86
- _webers = Convert . ToDouble ( webers ) ;
98
+ _value = Convert . ToDouble ( webers ) ;
99
+ Unit = BaseUnit ;
87
100
}
88
101
102
+ /// <summary>
103
+ /// Creates the quantity with the given numeric value and unit.
104
+ /// </summary>
105
+ /// <param name="numericValue">Numeric value.</param>
106
+ /// <param name="unit">Unit representation.</param>
107
+ /// <remarks>Value parameter cannot be named 'value' due to constraint when targeting Windows Runtime Component.</remarks>
108
+ #if WINDOWS_UWP
109
+ public MagneticFlux ( double numericValue , MagneticFluxUnit unit )
110
+ #else
111
+ public MagneticFlux ( double numericValue , MagneticFluxUnit unit )
112
+ #endif
113
+ {
114
+ _value = numericValue ;
115
+ Unit = unit ;
116
+ }
117
+
89
118
// Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods
90
119
#if WINDOWS_UWP
91
120
private
92
121
#else
122
+ [ Obsolete ( "Use the constructor that takes a unit parameter. This constructor will be removed in a future version." ) ]
93
123
public
94
124
#endif
95
- MagneticFlux ( long webers )
96
- {
97
- _webers = Convert . ToDouble ( webers ) ;
98
- }
125
+ MagneticFlux ( long webers ) : this ( Convert . ToDouble ( webers ) , BaseUnit ) { }
99
126
100
127
// Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods
101
128
// Windows Runtime Component does not support decimal type
102
129
#if WINDOWS_UWP
103
130
private
104
131
#else
132
+ [ Obsolete ( "Use the constructor that takes a unit parameter. This constructor will be removed in a future version." ) ]
105
133
public
106
134
#endif
107
- MagneticFlux ( decimal webers )
108
- {
109
- _webers = Convert . ToDouble ( webers ) ;
110
- }
135
+ MagneticFlux ( decimal webers ) : this ( Convert . ToDouble ( webers ) , BaseUnit ) { }
111
136
112
137
#region Properties
113
138
@@ -119,50 +144,36 @@ public MagneticFlux(double webers)
119
144
/// <summary>
120
145
/// The base unit representation of this quantity for the numeric value stored internally. All conversions go via this value.
121
146
/// </summary>
122
- public static MagneticFluxUnit BaseUnit
123
- {
124
- get { return MagneticFluxUnit . Weber ; }
125
- }
147
+ public static MagneticFluxUnit BaseUnit => MagneticFluxUnit . Weber ;
126
148
127
149
/// <summary>
128
150
/// All units of measurement for the MagneticFlux quantity.
129
151
/// </summary>
130
152
public static MagneticFluxUnit [ ] Units { get ; } = Enum . GetValues ( typeof ( MagneticFluxUnit ) ) . Cast < MagneticFluxUnit > ( ) . ToArray ( ) ;
131
-
132
153
/// <summary>
133
154
/// Get MagneticFlux in Webers.
134
155
/// </summary>
135
- public double Webers
136
- {
137
- get { return _webers ; }
138
- }
156
+ public double Webers => As ( MagneticFluxUnit . Weber ) ;
139
157
140
158
#endregion
141
159
142
160
#region Static
143
161
144
- public static MagneticFlux Zero
145
- {
146
- get { return new MagneticFlux ( ) ; }
147
- }
162
+ public static MagneticFlux Zero => new MagneticFlux ( 0 , BaseUnit ) ;
148
163
149
164
/// <summary>
150
165
/// Get MagneticFlux from Webers.
151
166
/// </summary>
152
167
#if WINDOWS_UWP
153
168
[ Windows . Foundation . Metadata . DefaultOverload ]
154
169
public static MagneticFlux FromWebers ( double webers )
155
- {
156
- double value = ( double ) webers ;
157
- return new MagneticFlux ( value ) ;
158
- }
159
170
#else
160
171
public static MagneticFlux FromWebers ( QuantityValue webers )
172
+ #endif
161
173
{
162
174
double value = ( double ) webers ;
163
- return new MagneticFlux ( ( value ) ) ;
175
+ return new MagneticFlux ( value , MagneticFluxUnit . Weber ) ;
164
176
}
165
- #endif
166
177
167
178
// Windows Runtime Component does not support nullable types (double?): https://msdn.microsoft.com/en-us/library/br230301.aspx
168
179
#if ! WINDOWS_UWP
@@ -197,14 +208,7 @@ public static MagneticFlux From(double value, MagneticFluxUnit fromUnit)
197
208
public static MagneticFlux From ( QuantityValue value , MagneticFluxUnit fromUnit )
198
209
#endif
199
210
{
200
- switch ( fromUnit )
201
- {
202
- case MagneticFluxUnit . Weber :
203
- return FromWebers ( value ) ;
204
-
205
- default :
206
- throw new NotImplementedException ( "fromUnit: " + fromUnit ) ;
207
- }
211
+ return new MagneticFlux ( ( double ) value , fromUnit ) ;
208
212
}
209
213
210
214
// Windows Runtime Component does not support nullable types (double?): https://msdn.microsoft.com/en-us/library/br230301.aspx
@@ -221,14 +225,8 @@ public static MagneticFlux From(QuantityValue value, MagneticFluxUnit fromUnit)
221
225
{
222
226
return null ;
223
227
}
224
- switch ( fromUnit )
225
- {
226
- case MagneticFluxUnit . Weber :
227
- return FromWebers ( value . Value ) ;
228
228
229
- default :
230
- throw new NotImplementedException ( "fromUnit: " + fromUnit ) ;
231
- }
229
+ return new MagneticFlux ( ( double ) value . Value , fromUnit ) ;
232
230
}
233
231
#endif
234
232
@@ -263,37 +261,37 @@ public static string GetAbbreviation(MagneticFluxUnit unit, [CanBeNull] Culture
263
261
#if ! WINDOWS_UWP
264
262
public static MagneticFlux operator - ( MagneticFlux right )
265
263
{
266
- return new MagneticFlux ( - right . _webers ) ;
264
+ return new MagneticFlux ( - right . Value , right . Unit ) ;
267
265
}
268
266
269
267
public static MagneticFlux operator + ( MagneticFlux left , MagneticFlux right )
270
268
{
271
- return new MagneticFlux ( left . _webers + right . _webers ) ;
269
+ return new MagneticFlux ( left . Value + right . AsBaseNumericType ( left . Unit ) , left . Unit ) ;
272
270
}
273
271
274
272
public static MagneticFlux operator - ( MagneticFlux left , MagneticFlux right )
275
273
{
276
- return new MagneticFlux ( left . _webers - right . _webers ) ;
274
+ return new MagneticFlux ( left . Value - right . AsBaseNumericType ( left . Unit ) , left . Unit ) ;
277
275
}
278
276
279
277
public static MagneticFlux operator * ( double left , MagneticFlux right )
280
278
{
281
- return new MagneticFlux ( left * right . _webers ) ;
279
+ return new MagneticFlux ( left * right . Value , right . Unit ) ;
282
280
}
283
281
284
282
public static MagneticFlux operator * ( MagneticFlux left , double right )
285
283
{
286
- return new MagneticFlux ( left . _webers * ( double ) right ) ;
284
+ return new MagneticFlux ( left . Value * right , left . Unit ) ;
287
285
}
288
286
289
287
public static MagneticFlux operator / ( MagneticFlux left , double right )
290
288
{
291
- return new MagneticFlux ( left . _webers / ( double ) right ) ;
289
+ return new MagneticFlux ( left . Value / right , left . Unit ) ;
292
290
}
293
291
294
292
public static double operator / ( MagneticFlux left , MagneticFlux right )
295
293
{
296
- return Convert . ToDouble ( left . _webers / right . _webers ) ;
294
+ return left . Webers / right . Webers ;
297
295
}
298
296
#endif
299
297
@@ -316,43 +314,43 @@ public int CompareTo(object obj)
316
314
#endif
317
315
int CompareTo ( MagneticFlux other )
318
316
{
319
- return _webers . CompareTo ( other . _webers ) ;
317
+ return AsBaseUnitWebers ( ) . CompareTo ( other . AsBaseUnitWebers ( ) ) ;
320
318
}
321
319
322
320
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx
323
321
#if ! WINDOWS_UWP
324
322
public static bool operator <= ( MagneticFlux left , MagneticFlux right )
325
323
{
326
- return left . _webers <= right . _webers ;
324
+ return left . Value <= right . AsBaseNumericType ( left . Unit ) ;
327
325
}
328
326
329
327
public static bool operator >= ( MagneticFlux left , MagneticFlux right )
330
328
{
331
- return left . _webers >= right . _webers ;
329
+ return left . Value >= right . AsBaseNumericType ( left . Unit ) ;
332
330
}
333
331
334
332
public static bool operator < ( MagneticFlux left , MagneticFlux right )
335
333
{
336
- return left . _webers < right . _webers ;
334
+ return left . Value < right . AsBaseNumericType ( left . Unit ) ;
337
335
}
338
336
339
337
public static bool operator > ( MagneticFlux left , MagneticFlux right )
340
338
{
341
- return left . _webers > right . _webers ;
339
+ return left . Value > right . AsBaseNumericType ( left . Unit ) ;
342
340
}
343
341
344
342
[ Obsolete ( "It is not safe to compare equality due to using System.Double as the internal representation. It is very easy to get slightly different values due to floating point operations. Instead use Equals(other, maxError) to provide the max allowed error." ) ]
345
343
public static bool operator == ( MagneticFlux left , MagneticFlux right )
346
344
{
347
345
// ReSharper disable once CompareOfFloatsByEqualityOperator
348
- return left . _webers == right . _webers ;
346
+ return left . Value == right . AsBaseNumericType ( left . Unit ) ;
349
347
}
350
348
351
349
[ Obsolete ( "It is not safe to compare equality due to using System.Double as the internal representation. It is very easy to get slightly different values due to floating point operations. Instead use Equals(other, maxError) to provide the max allowed error." ) ]
352
350
public static bool operator != ( MagneticFlux left , MagneticFlux right )
353
351
{
354
352
// ReSharper disable once CompareOfFloatsByEqualityOperator
355
- return left . _webers != right . _webers ;
353
+ return left . Value != right . AsBaseNumericType ( left . Unit ) ;
356
354
}
357
355
#endif
358
356
@@ -364,7 +362,7 @@ public override bool Equals(object obj)
364
362
return false ;
365
363
}
366
364
367
- return _webers . Equals ( ( ( MagneticFlux ) obj ) . _webers ) ;
365
+ return Value . Equals ( ( ( MagneticFlux ) obj ) . AsBaseNumericType ( Unit ) ) ;
368
366
}
369
367
370
368
/// <summary>
@@ -377,12 +375,12 @@ public override bool Equals(object obj)
377
375
/// <returns>True if the difference between the two values is not greater than the specified max.</returns>
378
376
public bool Equals ( MagneticFlux other , MagneticFlux maxError )
379
377
{
380
- return Math . Abs ( _webers - other . _webers ) <= maxError . _webers ;
378
+ return Math . Abs ( Value - other . AsBaseNumericType ( Unit ) ) <= maxError . AsBaseNumericType ( Unit ) ;
381
379
}
382
380
383
381
public override int GetHashCode ( )
384
382
{
385
- return _webers . GetHashCode ( ) ;
383
+ return new { Value , Unit } . GetHashCode ( ) ;
386
384
}
387
385
388
386
#endregion
@@ -392,14 +390,19 @@ public override int GetHashCode()
392
390
/// <summary>
393
391
/// Convert to the unit representation <paramref name="unit" />.
394
392
/// </summary>
395
- /// <returns>Value in new unit if successful, exception otherwise.</returns>
396
- /// <exception cref="NotImplementedException">If conversion was not successful.</exception>
393
+ /// <returns>Value converted to the specified unit.</returns>
397
394
public double As ( MagneticFluxUnit unit )
398
395
{
396
+ if ( Unit == unit )
397
+ {
398
+ return ( double ) Value ;
399
+ }
400
+
401
+ double baseUnitValue = AsBaseUnitWebers ( ) ;
402
+
399
403
switch ( unit )
400
404
{
401
- case MagneticFluxUnit . Weber :
402
- return Webers ;
405
+ case MagneticFluxUnit . Weber : return baseUnitValue ;
403
406
404
407
default :
405
408
throw new NotImplementedException ( "unit: " + unit ) ;
@@ -576,6 +579,7 @@ static MagneticFluxUnit ParseUnit(string str, IFormatProvider formatProvider = n
576
579
577
580
#endregion
578
581
582
+ [ Obsolete ( "This should no longer be used. ToString() uses the unit this quantity value was constructed with as default. Pass argument BaseUnit to preserve old behavior." ) ]
579
583
/// <summary>
580
584
/// Set the default unit used by ToString(). Default is Weber
581
585
/// </summary>
@@ -587,7 +591,7 @@ static MagneticFluxUnit ParseUnit(string str, IFormatProvider formatProvider = n
587
591
/// <returns>String representation.</returns>
588
592
public override string ToString ( )
589
593
{
590
- return ToString ( ToStringDefaultUnit ) ;
594
+ return ToString ( Unit ) ;
591
595
}
592
596
593
597
/// <summary>
@@ -655,23 +659,31 @@ public string ToString(MagneticFluxUnit unit, [CanBeNull] Culture culture, [NotN
655
659
/// <summary>
656
660
/// Represents the largest possible value of MagneticFlux
657
661
/// </summary>
658
- public static MagneticFlux MaxValue
659
- {
660
- get
661
- {
662
- return new MagneticFlux ( double . MaxValue ) ;
663
- }
664
- }
662
+ public static MagneticFlux MaxValue => new MagneticFlux ( double . MaxValue , BaseUnit ) ;
665
663
666
664
/// <summary>
667
665
/// Represents the smallest possible value of MagneticFlux
668
666
/// </summary>
669
- public static MagneticFlux MinValue
667
+ public static MagneticFlux MinValue => new MagneticFlux ( double . MinValue , BaseUnit ) ;
668
+
669
+ /// <summary>
670
+ /// Converts the current value + unit to the base unit.
671
+ /// This is typically the first step in converting from one unit to another.
672
+ /// </summary>
673
+ /// <returns>The value in the base unit representation.</returns>
674
+ private double AsBaseUnitWebers ( )
670
675
{
671
- get
676
+ if ( Unit == MagneticFluxUnit . Weber ) { return _value ; }
677
+
678
+ switch ( Unit )
672
679
{
673
- return new MagneticFlux ( double . MinValue ) ;
674
- }
675
- }
676
- }
680
+ case MagneticFluxUnit . Weber : return _value ;
681
+ default :
682
+ throw new NotImplementedException ( "Unit not implemented: " + Unit ) ;
683
+ }
684
+ }
685
+
686
+ /// <summary>Convenience method for working with internal numeric type.</summary>
687
+ private double AsBaseNumericType ( MagneticFluxUnit unit ) => Convert . ToDouble ( As ( unit ) ) ;
688
+ }
677
689
}
0 commit comments