When the string is invalid they return a fallback value.
public static class StringParserExtensions { public static int ParseInt(this string part, int fallbackValue = 0) { if (!int.TryParse(part.Trim(), out int anInt)) { anInt = fallbackValue; } return anInt; } public static bool ParseBool(this string part, bool fallbackValue = false) { if (!bool.TryParse(part.Trim(), out bool aBool)) { aBool = fallbackValue; } return aBool; } public static float ParseFloat(this string part, float fallbackValue = 0.0f) { if (!float.TryParse(part.Trim(), out float number)) { number = fallbackValue; } return number; } }Example usage:
stationLocation.StationId = parts[0].ParseInt(0);
No comments:
Post a Comment