/// <summary> /// Gets a value from the ini file. /// </summary> /// <typeparam name="T">The type of value to return.</typeparam> /// <param name="section">The section the value belongs to.</param> /// <param name="key">The key to the value.</param> /// <param name="defaultValue">The default value to return if no value was found. Optional.</param> /// <returns>The value, or defaultValue if the value doesn't exist.</returns> public T GetValue <T>(string section, string key, T defaultValue = default(T)) { if (!ValueExists(section, key)) { if ((defaultValue == null) && (typeof(T) == typeof(string))) { return((T)Convert.ChangeType("", typeof(T))); } return(defaultValue); } object val = ReadValue(section, key) ?? ""; try { if (typeof(T) == typeof(string)) { val = val.ToString(); } else if (typeof(T) == typeof(bool)) { val = HQTools.StringToBool((string)val); } else if (typeof(T) == typeof(int)) { val = HQTools.StringToInt((string)val); } else if (typeof(T) == typeof(float)) { val = HQTools.StringToFloat((string)val); } else if (typeof(T) == typeof(double)) { val = HQTools.StringToDouble((string)val); } else if (typeof(T) == typeof(Coordinates)) { val = new Coordinates(val.ToString()); } else if (typeof(T) == typeof(MinMaxI)) { val = new MinMaxI(val.ToString()); } else if (typeof(T) == typeof(MinMaxD)) { val = new MinMaxD(val.ToString()); } else if (typeof(T).IsEnum) { val = Enum.Parse(typeof(T), val.ToString(), true); } return((T)Convert.ChangeType(val, typeof(T))); } catch (Exception) { return(default(T)); } }
/// <summary> /// Constructor. Parses the X and Y coordinates from a string (format is "1.2345,6.7890") /// </summary> /// <param name="coordinatesString">The string containing the coordinates.</param> public Coordinates(string coordinatesString) { try { string[] xAndYStrings = coordinatesString.Split(','); X = HQTools.StringToDouble(xAndYStrings[0]); Y = HQTools.StringToDouble(xAndYStrings[1]); } catch (Exception) { X = 0; Y = 0; } }
/// <summary> /// Constructor. Parses the min and max coordinates from a string (format is "1.2345,6.7890") /// </summary> /// <param name="minMaxString">The string containing the min and max value.</param> public MinMaxD(string minMaxString) { try { string[] minAndMaxString = minMaxString.Split(','); double val1 = HQTools.StringToDouble(minAndMaxString[0]); double val2 = HQTools.StringToDouble(minAndMaxString[1]); Min = Math.Min(val1, val2); Max = Math.Max(val1, val2); } catch (Exception) { Min = 0; Max = 0; } }
/// <summary> /// Converts a string array to an array of type T. /// </summary> /// <typeparam name="T">The type of the source array.</typeparam> /// <param name="sourceArray">The source string array.</param> /// <returns>The converted array</returns> private T[] ConvertArray <T>(string[] sourceArray) { try { T[] arr = new T[sourceArray.Length]; for (int i = 0; i < sourceArray.Length; i++) { object o = default(T); if (typeof(T) == typeof(bool)) { o = HQTools.StringToBool(sourceArray[i]); } else if (typeof(T) == typeof(int)) { o = HQTools.StringToInt(sourceArray[i]); } else if (typeof(T) == typeof(double)) { o = HQTools.StringToDouble(sourceArray[i]); } else if (typeof(T) == typeof(float)) { o = HQTools.StringToFloat(sourceArray[i]); } else if (typeof(T).IsEnum) { o = Enum.Parse(typeof(T), sourceArray[i].ToString(), true); } arr[i] = (T)Convert.ChangeType(o, typeof(T)); } return(arr); } catch (Exception) { return(new T[0]); } }