using System; using System.Collections.Generic; using System.Text; namespace Project.YourNamespace { /// /// Provides parsing functionality for value types. /// public static class NullableParser { #region private delegate private delegate bool TryParseDelegate(string s, out T result) where T : struct; #endregion #region private methods private static bool TryParseNullable(string value, out Nullable result, TryParseDelegate tryParse, bool alwaysNull) where T : struct { if(string.IsNullOrEmpty(value)) { if(alwaysNull) { result = null; return false; } else { result = default(T); return false; } } T tempResult; bool success = tryParse(value, out tempResult); result = tempResult; return success; } #endregion #region byte TryParse /// /// Converts the string representation of a number to its byte equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a number to convert. /// When this method returns, contains the byte value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out byte? result, bool alwaysNull) { return TryParseNullable(value, out result, byte.TryParse, alwaysNull); } /// /// Converts the string representation of a number to its byte equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a number to convert. /// When this method returns, contains the byte value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out byte? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region sbyte TryParse /// /// Converts the string representation of a number to its sbyte equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a number to convert. /// When this method returns, contains the sbyte value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. [method: CLSCompliant(false)] public static bool TryParse(string value, out sbyte? result, bool alwaysNull) { return TryParseNullable(value, out result, sbyte.TryParse, alwaysNull); } /// /// Converts the string representation of a number to its sbyte equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a number to convert. /// When this method returns, contains the sbyte value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. [method: CLSCompliant(false)] public static bool TryParse(string value, out sbyte? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region short TryParse /// /// Converts the string representation of a number to its 16-bit signed integer equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a number to convert. /// When this method returns, contains the 16-bit signed integer value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out short? result, bool alwaysNull) { return TryParseNullable(value, out result, short.TryParse, alwaysNull); } /// /// Converts the string representation of a number to its 16-bit signed integer equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a number to convert. /// When this method returns, contains the 16-bit signed integer value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out short? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region ushort TryParse /// /// Converts the string representation of a number to its 16-bit unsigned integer equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a number to convert. /// When this method returns, contains the 16-bit unsigned integer value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. [method: CLSCompliant(false)] public static bool TryParse(string value, out ushort? result, bool alwaysNull) { return TryParseNullable(value, out result, ushort.TryParse, alwaysNull); } /// /// Converts the string representation of a number to its 16-bit unsigned integer equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a number to convert. /// When this method returns, contains the 16-bit unsigned integer value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. [method: CLSCompliant(false)] public static bool TryParse(string value, out ushort? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region int TryParse /// /// Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded. /// /// A string containing a number to convert. /// When this method returns, contains the 32-bit signed integer value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out int? result, bool alwaysNull) { return TryParseNullable(value, out result, int.TryParse, alwaysNull); } /// /// Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded. /// /// A string containing a number to convert. /// When this method returns, contains the 32-bit signed integer value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out int? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region uint TryParse /// /// Converts the string representation of a number to its 32-bit unsigned integer equivalent. A return value indicates whether the operation succeeded. /// /// A string containing a number to convert. /// When this method returns, contains the 32-bit unsigned integer value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. [method: CLSCompliant(false)] public static bool TryParse(string value, out uint? result, bool alwaysNull) { return TryParseNullable(value, out result, uint.TryParse, alwaysNull); } /// /// Converts the string representation of a number to its 32-bit unsigned integer equivalent. A return value indicates whether the operation succeeded. /// /// A string containing a number to convert. /// When this method returns, contains the 32-bit unsigned integer value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. [method: CLSCompliant(false)] public static bool TryParse(string value, out uint? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region long TryParse /// /// Converts the string representation of a number to its 64-bit signed integer equivalent. A return value indicates whether the operation succeeded. /// /// A string containing a number to convert. /// When this method returns, contains the 64-bit signed integer value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out long? result, bool alwaysNull) { return TryParseNullable(value, out result, long.TryParse, alwaysNull); } /// /// Converts the string representation of a number to its 64-bit signed integer equivalent. A return value indicates whether the operation succeeded. /// /// A string containing a number to convert. /// When this method returns, contains the 64-bit signed integer value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out long? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region ulong TryParse /// /// Converts the string representation of a number to its 64-bit unsigned integer equivalent. A return value indicates whether the operation succeeded. /// /// A string containing a number to convert. /// When this method returns, contains the 64-bit unsigned integer value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. [method: CLSCompliant(false)] public static bool TryParse(string value, out ulong? result, bool alwaysNull) { return TryParseNullable(value, out result, ulong.TryParse, alwaysNull); } /// /// Converts the string representation of a number to its 64-bit unsigned integer equivalent. A return value indicates whether the operation succeeded. /// /// A string containing a number to convert. /// When this method returns, contains the 64-bit unsigned integer value equivalent to the number contained in value, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. [method: CLSCompliant(false)] public static bool TryParse(string value, out ulong? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region decimal TryParse /// /// Converts the String representation of a number to its Decimal equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a number to convert. /// When this method returns, contains the Decimal number that is equivalent to the numeric value contained in value, if the conversion succeeded, or is zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not a number in a valid format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out decimal? result, bool alwaysNull) { return TryParseNullable(value, out result, decimal.TryParse, alwaysNull); } /// /// Converts the String representation of a number to its Decimal equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a number to convert. /// When this method returns, contains the Decimal number that is equivalent to the numeric value contained in s, if the conversion succeeded, or is zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not a number in a valid format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out decimal? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region double TryParse /// /// Converts the String representation of a number to its Double equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a number to convert. /// When this method returns, contains the Double number that is equivalent to the numeric value contained in value, if the conversion succeeded, or is zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not a number in a valid format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out double? result, bool alwaysNull) { return TryParseNullable(value, out result, double.TryParse, alwaysNull); } /// /// Converts the String representation of a number to its Double equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a number to convert. /// When this method returns, contains the Double number that is equivalent to the numeric value contained in value, if the conversion succeeded, or is zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not a number in a valid format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out double? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region single TryParse /// /// Converts the String representation of a number to its Single equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a number to convert. /// When this method returns, contains the Single number that is equivalent to the numeric value contained in value, if the conversion succeeded, or is zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not a number in a valid format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out Single? result, bool alwaysNull) { return TryParseNullable(value, out result, Single.TryParse, alwaysNull); } /// /// Converts the String representation of a number to its Single equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a number to convert. /// When this method returns, contains the Single number that is equivalent to the numeric value contained in value, if the conversion succeeded, or is zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not a number in a valid format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out Single? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region boolean TryParse /// /// Converts the specified string representation of a logical value to its Boolean equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a value to convert. /// When this method returns, if the conversion succeeded, contains true if value is equivalent to TrueString or false if value is equivalent to FalseString. If the conversion failed, contains false. The conversion fails if value is a null reference (Nothing in Visual Basic) or is not equivalent to either TrueString or FalseString. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out bool? result, bool alwaysNull) { return TryParseNullable(value, out result, bool.TryParse, alwaysNull); } /// /// Converts the specified string representation of a logical value to its Boolean equivalent. A return value indicates whether the conversion succeeded or failed. /// /// A string containing a value to convert. /// When this method returns, if the conversion succeeded, contains true if value is equivalent to TrueString or false if value is equivalent to FalseString. If the conversion failed, contains false. The conversion fails if value is a null reference (Nothing in Visual Basic) or is not equivalent to either TrueString or FalseString. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out bool? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region char TryParse /// /// Converts the value of the specified string to its equivalent Unicode character. A return code indicates whether the conversion succeeded or failed. /// /// A string containing a single character or a null reference (Nothing in Visual Basic). /// When this method returns, contains a Unicode character equivalent to the sole character in value, if the conversion succeeded, or an undefined value if the conversion failed. The conversion fails if the value parameter is a null reference (Nothing in Visual Basic) or the length of value is not 1. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out char? result, bool alwaysNull) { return TryParseNullable(value, out result, char.TryParse, alwaysNull); } /// /// Converts the value of the specified string to its equivalent Unicode character. A return code indicates whether the conversion succeeded or failed. /// /// A string containing a single character or a null reference (Nothing in Visual Basic). /// When this method returns, contains a Unicode character equivalent to the sole character in value, if the conversion succeeded, or an undefined value if the conversion failed. The conversion fails if the value parameter is a null reference (Nothing in Visual Basic) or the length of value is not 1. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out char? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region DateTime TryParse /// /// Converts the specified string representation of a date and time to its DateTime equivalent. /// /// A string containing a date and time to convert. /// When this method returns, contains the DateTime value equivalent to the date and time contained in value, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the value parameter is a null reference (Nothing in Visual Basic), or does not contain a valid string representation of a date and time. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out DateTime? result, bool alwaysNull) { return TryParseNullable(value, out result, DateTime.TryParse, alwaysNull); } /// /// Converts the specified string representation of a date and time to its DateTime equivalent. /// /// A string containing a date and time to convert. /// When this method returns, contains the DateTime value equivalent to the date and time contained in value, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the value parameter is a null reference (Nothing in Visual Basic), or does not contain a valid string representation of a date and time. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out DateTime? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region DateTimeOffset TryParse /// /// Converts the specified string representation of a offset and time to its DateTimeOffset equivalent. /// /// A string containing a offset to convert. /// When this method returns, contains the DateTimeOffset value equivalent to the offset contained in value, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the value parameter is a null reference (Nothing in Visual Basic), or does not contain a valid string representation of an offset. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out DateTimeOffset? result, bool alwaysNull) { return TryParseNullable(value, out result, DateTimeOffset.TryParse, alwaysNull); } /// /// Converts the specified string representation of a offset and time to its DateTimeOffset equivalent. /// /// A string containing a offset to convert. /// When this method returns, contains the DateTimeOffset value equivalent to the offset contained in value, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the value parameter is a null reference (Nothing in Visual Basic), or does not contain a valid string representation of an offset. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out DateTimeOffset? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region TimeSpan TryParse /// /// Constructs a new TimeSpan object from a time interval specified in a string. Parameters specify the time interval and the variable where the new TimeSpan object is returned. /// /// A string that specifies a time interval. /// When this method returns, contains an object that represents the time interval specified by value, or Zero if the conversion failed. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out TimeSpan? result, bool alwaysNull) { return TryParseNullable(value, out result, TimeSpan.TryParse, alwaysNull); } /// /// Constructs a new TimeSpan object from a time interval specified in a string. Parameters specify the time interval and the variable where the new TimeSpan object is returned. /// /// A string that specifies a time interval. /// When this method returns, contains an object that represents the time interval specified by value, or Zero if the conversion failed. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out TimeSpan? result) { return NullableParser.TryParse(value, out result, false); } #endregion #region Guid TryParse /// /// Constructs a new guid struct from a guid (unique identifier) specified in a string. Parameters specify the guid and the variable where the new guid struct is returned. /// /// A string that specifies a valid guid. /// When this method returns, contains an object that represents the guid specified by value, or guid.empty if the conversion failed. This parameter is passed uninitialized. /// true to set the value of result to null if the value to parse is null or empty; otherwise false to have result set using the default value of the type. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out Guid? result, bool alwaysNull) { result = Guid.Empty; if (string.IsNullOrEmpty(value)) { if (alwaysNull) { result = null; return false; } else { return false; } } try { result = new Guid(value); return true; } catch { return false; } } /// /// Constructs a new guid struct from a guid (unique identifier) specified in a string. Parameters specify the guid and the variable where the new guid struct is returned. /// /// A string that specifies a valid guid. /// When this method returns, contains an object that represents the guid specified by value, or guid.empty if the conversion failed. This parameter is passed uninitialized. /// true if the value parameter was converted successfully; otherwise, false. public static bool TryParse(string value, out Guid? result) { return NullableParser.TryParse(value, out result, false); } #endregion } }