Skip to content

Commit 27a0ad7

Browse files
committed
Add TryParse() methods
1 parent 71c7bca commit 27a0ad7

37 files changed

+1332
-0
lines changed

UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,42 @@ public static Acceleration Parse(string str, [CanBeNull] Culture culture)
627627
}, (x, y) => FromMeterPerSecondSquared(x.MeterPerSecondSquared + y.MeterPerSecondSquared));
628628
}
629629

630+
/// <summary>
631+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
632+
/// </summary>
633+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
634+
/// <param name="result">Resulting unit quantity if successful.</param>
635+
/// <example>
636+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
637+
/// </example>
638+
public static bool Parse([CanBeNull] string str, out Acceleration result)
639+
{
640+
return TryParse(str, null, out result);
641+
}
642+
643+
/// <summary>
644+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
645+
/// </summary>
646+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
647+
/// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param>
648+
/// <param name="result">Resulting unit quantity if successful.</param>
649+
/// <example>
650+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
651+
/// </example>
652+
public static bool TryParse([CanBeNull] string str, [CanBeNull] Culture culture, out Acceleration result)
653+
{
654+
try
655+
{
656+
result = Parse(str, culture);
657+
return true;
658+
}
659+
catch
660+
{
661+
result = default(Acceleration);
662+
return false;
663+
}
664+
}
665+
630666
/// <summary>
631667
/// Parse a unit string.
632668
/// </summary>

UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,42 @@ public static AmplitudeRatio Parse(string str, [CanBeNull] Culture culture)
487487
}, (x, y) => FromDecibelVolts(x.DecibelVolts + y.DecibelVolts));
488488
}
489489

490+
/// <summary>
491+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
492+
/// </summary>
493+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
494+
/// <param name="result">Resulting unit quantity if successful.</param>
495+
/// <example>
496+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
497+
/// </example>
498+
public static bool Parse([CanBeNull] string str, out AmplitudeRatio result)
499+
{
500+
return TryParse(str, null, out result);
501+
}
502+
503+
/// <summary>
504+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
505+
/// </summary>
506+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
507+
/// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param>
508+
/// <param name="result">Resulting unit quantity if successful.</param>
509+
/// <example>
510+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
511+
/// </example>
512+
public static bool TryParse([CanBeNull] string str, [CanBeNull] Culture culture, out AmplitudeRatio result)
513+
{
514+
try
515+
{
516+
result = Parse(str, culture);
517+
return true;
518+
}
519+
catch
520+
{
521+
result = default(AmplitudeRatio);
522+
return false;
523+
}
524+
}
525+
490526
/// <summary>
491527
/// Parse a unit string.
492528
/// </summary>

UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,42 @@ public static Angle Parse(string str, [CanBeNull] Culture culture)
849849
}, (x, y) => FromDegrees(x.Degrees + y.Degrees));
850850
}
851851

852+
/// <summary>
853+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
854+
/// </summary>
855+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
856+
/// <param name="result">Resulting unit quantity if successful.</param>
857+
/// <example>
858+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
859+
/// </example>
860+
public static bool Parse([CanBeNull] string str, out Angle result)
861+
{
862+
return TryParse(str, null, out result);
863+
}
864+
865+
/// <summary>
866+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
867+
/// </summary>
868+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
869+
/// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param>
870+
/// <param name="result">Resulting unit quantity if successful.</param>
871+
/// <example>
872+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
873+
/// </example>
874+
public static bool TryParse([CanBeNull] string str, [CanBeNull] Culture culture, out Angle result)
875+
{
876+
try
877+
{
878+
result = Parse(str, culture);
879+
return true;
880+
}
881+
catch
882+
{
883+
result = default(Angle);
884+
return false;
885+
}
886+
}
887+
852888
/// <summary>
853889
/// Parse a unit string.
854890
/// </summary>

UnitsNet/GeneratedCode/UnitClasses/Area.g.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,42 @@ public static Area Parse(string str, [CanBeNull] Culture culture)
738738
}, (x, y) => FromSquareMeters(x.SquareMeters + y.SquareMeters));
739739
}
740740

741+
/// <summary>
742+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
743+
/// </summary>
744+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
745+
/// <param name="result">Resulting unit quantity if successful.</param>
746+
/// <example>
747+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
748+
/// </example>
749+
public static bool Parse([CanBeNull] string str, out Area result)
750+
{
751+
return TryParse(str, null, out result);
752+
}
753+
754+
/// <summary>
755+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
756+
/// </summary>
757+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
758+
/// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param>
759+
/// <param name="result">Resulting unit quantity if successful.</param>
760+
/// <example>
761+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
762+
/// </example>
763+
public static bool TryParse([CanBeNull] string str, [CanBeNull] Culture culture, out Area result)
764+
{
765+
try
766+
{
767+
result = Parse(str, culture);
768+
return true;
769+
}
770+
catch
771+
{
772+
result = default(Area);
773+
return false;
774+
}
775+
}
776+
741777
/// <summary>
742778
/// Parse a unit string.
743779
/// </summary>

UnitsNet/GeneratedCode/UnitClasses/BrakeSpecificFuelConsumption.g.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,42 @@ public static BrakeSpecificFuelConsumption Parse(string str, [CanBeNull] Culture
479479
}, (x, y) => FromKilogramsPerJoule(x.KilogramsPerJoule + y.KilogramsPerJoule));
480480
}
481481

482+
/// <summary>
483+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
484+
/// </summary>
485+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
486+
/// <param name="result">Resulting unit quantity if successful.</param>
487+
/// <example>
488+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
489+
/// </example>
490+
public static bool Parse([CanBeNull] string str, out BrakeSpecificFuelConsumption result)
491+
{
492+
return TryParse(str, null, out result);
493+
}
494+
495+
/// <summary>
496+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
497+
/// </summary>
498+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
499+
/// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param>
500+
/// <param name="result">Resulting unit quantity if successful.</param>
501+
/// <example>
502+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
503+
/// </example>
504+
public static bool TryParse([CanBeNull] string str, [CanBeNull] Culture culture, out BrakeSpecificFuelConsumption result)
505+
{
506+
try
507+
{
508+
result = Parse(str, culture);
509+
return true;
510+
}
511+
catch
512+
{
513+
result = default(BrakeSpecificFuelConsumption);
514+
return false;
515+
}
516+
}
517+
482518
/// <summary>
483519
/// Parse a unit string.
484520
/// </summary>

UnitsNet/GeneratedCode/UnitClasses/Density.g.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,42 @@ public static Density Parse(string str, [CanBeNull] Culture culture)
738738
}, (x, y) => FromKilogramsPerCubicMeter(x.KilogramsPerCubicMeter + y.KilogramsPerCubicMeter));
739739
}
740740

741+
/// <summary>
742+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
743+
/// </summary>
744+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
745+
/// <param name="result">Resulting unit quantity if successful.</param>
746+
/// <example>
747+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
748+
/// </example>
749+
public static bool Parse([CanBeNull] string str, out Density result)
750+
{
751+
return TryParse(str, null, out result);
752+
}
753+
754+
/// <summary>
755+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
756+
/// </summary>
757+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
758+
/// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param>
759+
/// <param name="result">Resulting unit quantity if successful.</param>
760+
/// <example>
761+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
762+
/// </example>
763+
public static bool TryParse([CanBeNull] string str, [CanBeNull] Culture culture, out Density result)
764+
{
765+
try
766+
{
767+
result = Parse(str, culture);
768+
return true;
769+
}
770+
catch
771+
{
772+
result = default(Density);
773+
return false;
774+
}
775+
}
776+
741777
/// <summary>
742778
/// Parse a unit string.
743779
/// </summary>

UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,42 @@ public static Duration Parse(string str, [CanBeNull] Culture culture)
738738
}, (x, y) => FromSeconds(x.Seconds + y.Seconds));
739739
}
740740

741+
/// <summary>
742+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
743+
/// </summary>
744+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
745+
/// <param name="result">Resulting unit quantity if successful.</param>
746+
/// <example>
747+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
748+
/// </example>
749+
public static bool Parse([CanBeNull] string str, out Duration result)
750+
{
751+
return TryParse(str, null, out result);
752+
}
753+
754+
/// <summary>
755+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
756+
/// </summary>
757+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
758+
/// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param>
759+
/// <param name="result">Resulting unit quantity if successful.</param>
760+
/// <example>
761+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
762+
/// </example>
763+
public static bool TryParse([CanBeNull] string str, [CanBeNull] Culture culture, out Duration result)
764+
{
765+
try
766+
{
767+
result = Parse(str, culture);
768+
return true;
769+
}
770+
catch
771+
{
772+
result = default(Duration);
773+
return false;
774+
}
775+
}
776+
741777
/// <summary>
742778
/// Parse a unit string.
743779
/// </summary>

UnitsNet/GeneratedCode/UnitClasses/DynamicViscosity.g.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,42 @@ public static DynamicViscosity Parse(string str, [CanBeNull] Culture culture)
553553
}, (x, y) => FromNewtonSecondsPerMeterSquared(x.NewtonSecondsPerMeterSquared + y.NewtonSecondsPerMeterSquared));
554554
}
555555

556+
/// <summary>
557+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
558+
/// </summary>
559+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
560+
/// <param name="result">Resulting unit quantity if successful.</param>
561+
/// <example>
562+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
563+
/// </example>
564+
public static bool Parse([CanBeNull] string str, out DynamicViscosity result)
565+
{
566+
return TryParse(str, null, out result);
567+
}
568+
569+
/// <summary>
570+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
571+
/// </summary>
572+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
573+
/// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param>
574+
/// <param name="result">Resulting unit quantity if successful.</param>
575+
/// <example>
576+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
577+
/// </example>
578+
public static bool TryParse([CanBeNull] string str, [CanBeNull] Culture culture, out DynamicViscosity result)
579+
{
580+
try
581+
{
582+
result = Parse(str, culture);
583+
return true;
584+
}
585+
catch
586+
{
587+
result = default(DynamicViscosity);
588+
return false;
589+
}
590+
}
591+
556592
/// <summary>
557593
/// Parse a unit string.
558594
/// </summary>

UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,42 @@ public static ElectricCurrent Parse(string str, [CanBeNull] Culture culture)
590590
}, (x, y) => FromAmperes(x.Amperes + y.Amperes));
591591
}
592592

593+
/// <summary>
594+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
595+
/// </summary>
596+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
597+
/// <param name="result">Resulting unit quantity if successful.</param>
598+
/// <example>
599+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
600+
/// </example>
601+
public static bool Parse([CanBeNull] string str, out ElectricCurrent result)
602+
{
603+
return TryParse(str, null, out result);
604+
}
605+
606+
/// <summary>
607+
/// Try to parse a string with one or two quantities of the format "&lt;quantity&gt; &lt;unit&gt;".
608+
/// </summary>
609+
/// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
610+
/// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param>
611+
/// <param name="result">Resulting unit quantity if successful.</param>
612+
/// <example>
613+
/// Length.Parse("5.5 m", new CultureInfo("en-US"));
614+
/// </example>
615+
public static bool TryParse([CanBeNull] string str, [CanBeNull] Culture culture, out ElectricCurrent result)
616+
{
617+
try
618+
{
619+
result = Parse(str, culture);
620+
return true;
621+
}
622+
catch
623+
{
624+
result = default(ElectricCurrent);
625+
return false;
626+
}
627+
}
628+
593629
/// <summary>
594630
/// Parse a unit string.
595631
/// </summary>

0 commit comments

Comments
 (0)