kOS.Safe.Encapsulation.ScalarValue.TryParse C# (CSharp) Method

TryParse() public static method

public static TryParse ( string str, ScalarValue &result ) : bool
str string
result ScalarValue
return bool
        public static bool TryParse(string str, out ScalarValue result)
        {
            result = null; // default the out value to null

            bool needsDoubleParse = str.IndexOfAny(doubleCharacters) >= 0;

            if (needsDoubleParse)
            {
                return TryParseDouble(str, out result);
            }
            else
            {
                return TryParseInt(str, out result);
            }
        }

Usage Example

        /// <summary>
        /// Parse the string into a number
        /// </summary>
        /// <param name="defaultIfError">If the string parse fails, return this value instead.  Note that if
        /// this optional value is left off, a KOSexception will be thrown on parsing errors instead.</param>
        /// <returns></returns>
        public ScalarValue ToScalar(ScalarValue defaultIfError = null)
        {
            ScalarValue result;

            if (ScalarValue.TryParse(internalString, out result))
            {
                return(result);
            }
            else if (defaultIfError != null)
            {
                return(defaultIfError);
            }

            throw new KOSNumberParseException(internalString);
        }