public static string ReturnEmptyStringIfNullElseValue(string value) { if (value == null) { return ""; } else { return value.ToString().Trim(); } }
That isn't the worst, most useless block of code possible. Neither was his method to turn string
s into int
s.
public static int ReturnIntValueOfString(string value) { if (value != null) { if (value.ToString().Trim().Length == 0) return 0; else { int tmpValue = 0; // This will fail if a decimal number is passed in if (int.TryParse(value.ToString().Trim().Replace("$", "").Replace(",", ""), out tmpValue)) return tmpValue; // Let's handle decimals now decimal tmpDecimal = 0; if (decimal.TryParse(value.ToString().Trim().Replace("$", "").Replace(",", ""), out tmpDecimal)) tmpValue = Convert.ToInt32(Math.Round(tmpDecimal, 0)); return tmpValue; } } else return 0; }
Or these twin functions:
public static decimal ReturnDecimalValueOfString(string value) { if (value != null) { if (value.ToString().Trim().Length == 0) { return 0M; } else { decimal tmpValue = 0M; decimal.TryParse(value.ToString().Trim().Replace("$", "").Replace(",", ""), out tmpValue); return tmpValue; } } else { return 0M; } } |
public static byte ReturnByteValueOfString(string value) { if (value != null) { if (value.ToString().Trim().Length == 0) { return 0; } else { byte tmpValue = 0; byte.TryParse(value.ToString().Trim().Replace("$", "").Replace(",", ""), out tmpValue); return tmpValue; } } else { return 0; } } |
Now, Java doesn't have a built-in "isNumeric " function. Ben's codebase wasn't actually in Java, but Ben's predecessor had to implement his own anyway. He found one of the more… clever approaches to the problem.
public static bool isNumeric(string value) { bool isNum = true; for (int i = 0; i < value.Length; i++) { if (!"1234567890".Contains(value.Substring(i, 1))) { isNum = false; } } return isNum; }Hey, you. Yeah you. The one reader on Google+. I don't think you've followed us yet.