Skip to content

Commit a256eab

Browse files
committed
Fix some ambiguity issues
1 parent 2beb8e3 commit a256eab

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

UnitsNet.Tests/CustomCode/MassConcentrationTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public static void ComponentMassFromMassConcentrationAndSolutionVolume(
131131
var massConcentration = new MassConcentration(massConcValue, massConcUnit);
132132
var volume = new Volume(volumeValue, volumeUnit);
133133

134-
Mass massComponent = massConcentration * volume;
134+
Mass massComponent = (Density)massConcentration * volume;
135135

136136
AssertEx.EqualTolerance(expectedMassValue, massComponent.As(expectedMassUnit), tolerance);
137137
}

UnitsNet/CustomCode/Quantities/MassConcentration.extra.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,29 @@ namespace UnitsNet
55
{
66
public partial struct MassConcentration
77
{
8+
/// <summary>
9+
/// Because Density and MassConcentration are almost the same, we can convert between them.
10+
/// </summary>
11+
public static implicit operator MassConcentration(Density density)
12+
{
13+
return From(density.ToUnit(Density.BaseUnit).Value, BaseUnit);
14+
}
15+
16+
/// <summary>
17+
/// Because Density and MassConcentration are almost the same, we can convert between them.
18+
/// </summary>
19+
public static explicit operator Density(MassConcentration massConcentration)
20+
{
21+
return Density.From(massConcentration.ToUnit(BaseUnit).Value, Density.BaseUnit);
22+
}
23+
824
/// <summary>
925
/// Get <see cref="Molarity" /> from this <see cref="MassConcentration" /> using the known component <see cref="MolarMass" />.
1026
/// </summary>
1127
/// <param name="molecularWeight"></param>
1228
public Molarity ToMolarity(MolarMass molecularWeight)
1329
{
14-
return this / molecularWeight;
30+
return (Density)this / molecularWeight;
1531
}
1632

1733
/// <summary>
@@ -21,7 +37,7 @@ public Molarity ToMolarity(MolarMass molecularWeight)
2137
/// <returns></returns>
2238
public VolumeConcentration ToVolumeConcentration(Density componentDensity)
2339
{
24-
return this / componentDensity;
40+
return VolumeConcentration.FromDecimalFractions((Density)this / componentDensity);
2541
}
2642

2743
#region Static Methods

UnitsNet/CustomCode/Quantities/MassFraction.extra.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public partial struct MassFraction
1111
/// <returns>The actual mass of the component involved in this mixture</returns>
1212
public Mass GetComponentMass(Mass totalMass)
1313
{
14-
return totalMass * this;
14+
return totalMass * DecimalFractions;
1515
}
1616

1717
/// <summary>
@@ -21,7 +21,7 @@ public Mass GetComponentMass(Mass totalMass)
2121
/// <returns>The total mass of the mixture</returns>
2222
public Mass GetTotalMass(Mass componentMass)
2323
{
24-
return componentMass / this;
24+
return componentMass / DecimalFractions;
2525
}
2626

2727
#region Static Methods

UnitsNet/CustomCode/Quantities/Molarity.extra.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public MassConcentration ToMassConcentration(MolarMass molecularWeight)
2020
/// <param name="componentMass"></param>
2121
public VolumeConcentration ToVolumeConcentration(Density componentDensity, MolarMass componentMass)
2222
{
23-
return this * componentMass / componentDensity;
23+
return VolumeConcentration.FromDecimalFractions(this * componentMass / componentDensity);
2424
}
2525

2626
#region Static Methods

UnitsNet/CustomCode/Quantities/VolumeConcentration.extra.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static VolumeConcentration FromVolumes(Volume componentVolume, Volume mix
4343
/// <param name="componentMolarMass"></param>
4444
public static VolumeConcentration FromMolarity(Molarity molarity, Density componentDensity, MolarMass componentMolarMass)
4545
{
46-
return molarity * componentMolarMass / componentDensity;
46+
return FromDecimalFractions(molarity * componentMolarMass / componentDensity);
4747
}
4848

4949
#endregion

0 commit comments

Comments
 (0)