예제 #1
0
파일: Speed.cs 프로젝트: cmberryau/core-3.5
        /// <summary>
        /// Tries to parse a string representing a speed. Assumes kilometers per hour by default, use explicit parsing methods for different behaviour.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool TryParse(string s, out Speed result)
        {
            result = null;

            if (s.IsNullOrWhiteSpace())
            {
                return(false);
            }

            // try a generic parse first, in this case assume kilometers per hour.
            double value;

            if (double.TryParse(s, out value))
            { // the value is just a numeric value.
                result = new KilometerPerHour(value);
                return(true);
            }

            // try kilometers per hour.
            if (KilometerPerHour.TryParse(s, out result))
            { // succes!
                return(true);
            }

            // try miles per hour.
            if (MilesPerHour.TryParse(s, out result))
            { // success!
                return(true);
            }

            // try knots.
            if (Knots.TryParse(s, out result))
            { // success!
                return(true);
            }

            // try meters per second.
            if (MeterPerSecond.TryParse(s, out result))
            { // success!
                return(true);
            }
            return(false);
        }
예제 #2
0
        public static bool TryParse(string s, out MilesPerHour result)
        {
            result = (MilesPerHour)null;
            double result1;

            if (double.TryParse(s, out result1))
            {
                result = new MilesPerHour(result1);
                return(true);
            }
            Match match = new Regex("^\\s*(\\d+(?:\\.\\d*)?)\\s*\\s*(mph)\\s*$", RegexOptions.IgnoreCase).Match(s);

            if (!match.Success)
            {
                return(false);
            }
            result = new MilesPerHour(double.Parse(match.Groups[1].Value, (IFormatProvider)CultureInfo.InvariantCulture));
            return(true);
        }
예제 #3
0
        /// <summary>
        /// Tries to parse a string containing a miles per hour value.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool TryParse(string s, out MilesPerHour result)
        {
            result = null;
            double value;
            if (double.TryParse(s, out value))
            { // the value is just a numeric value.
                result = new MilesPerHour(value);
                return true;
            }

            // do some more parsing work.
            Regex regex = new Regex("^" + Constants.RegexDecimalWhiteSpace + MilesPerHour.RegexUnitMilesPerHour + "$", RegexOptions.IgnoreCase);
            Match match = regex.Match(s);
            if (match.Success)
            {
                result = new MilesPerHour(double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture));
                return true;
            }
            return false;
        }
예제 #4
0
        /// <summary>
        /// Tries to parse a string containing a miles per hour value.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool TryParse(string s, out MilesPerHour result)
        {
            result = null;
            double value;

            if (double.TryParse(s, out value))
            { // the value is just a numeric value.
                result = new MilesPerHour(value);
                return(true);
            }

            // do some more parsing work.
            Regex regex = new Regex("^" + Constants.RegexDecimalWhiteSpace + MilesPerHour.RegexUnitMilesPerHour + "$", RegexOptions.IgnoreCase);
            Match match = regex.Match(s);

            if (match.Success)
            {
                result = new MilesPerHour(double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture));
                return(true);
            }
            return(false);
        }