/// <summary> /// Returns the integer value in the datasource or null if none is found. /// </summary> /// <param name="datasource">The datasource.</param> /// <param name="userDataSourceId">The user data source identifier.</param> /// <returns> /// The integer value in the datasource or null if none is found. /// </returns> /// <exception cref="InvalidOperationException"> /// Thrown when the value in the datasource cannot be converted to an integer. /// </exception> public static int?GetInt(this UserDataSources datasource, string userDataSourceId) { if (datasource.IsEmpty(userDataSourceId)) { return(null); } string data = datasource.GetString(userDataSourceId); int value; var success = int.TryParse(data, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out value); if (success) { return(value); } throw new InvalidOperationException(FormatMessage(userDataSourceId, data, typeof(int?))); }
public static DateTime?GetDateTime(this UserDataSources datasource, string userDataSourceId) { if (datasource.IsEmpty(userDataSourceId)) { return(null); } var data = datasource.GetString(userDataSourceId); DateTime value; var success = DateTime.TryParse(data, CultureInfo.InvariantCulture, DateTimeStyles.None, out value); if (success) { return(value); } throw new InvalidOperationException(FormatMessage(userDataSourceId, data, typeof(DateTime?))); }
/// <summary> /// Returns the double value in the datasource or null if none is found. /// </summary> /// <param name="datasource">The datasource.</param> /// <param name="userDataSourceId">The user data source identifier.</param> /// <returns> /// The double value in the datasource or null if none is found. /// </returns> /// <exception cref="InvalidOperationException"> /// Thrown when the value in the datasource cannot be converted to a double. /// </exception> public static double? GetDouble(this UserDataSources datasource, string userDataSourceId) { if (datasource.IsEmpty(userDataSourceId)) { return null; } string data = datasource.GetString(userDataSourceId); double value; var success = double.TryParse(data, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out value); if (success) { return value; } throw new InvalidOperationException(FormatMessage(userDataSourceId, data, typeof(double?))); }
/// <summary> /// Determines whether the specified user data source is empty. /// </summary> /// <param name="datasource">The datasource.</param> /// <param name="userDataSourceId">The user data source identifier.</param> /// <returns>True when empty, false otherwise.</returns> private static bool IsEmpty(this UserDataSources datasource, string userDataSourceId) { string value = datasource.GetString(userDataSourceId); return(string.IsNullOrEmpty(value)); }