Posts
231
Comments
64
Trackbacks
4
Wednesday, March 26, 2008
Kleine Funktion: ToValidInt

Hier eine kleine Funktion um ein beliebiges Objekt in einen gültigen Integer-Wert umzuwandeln:

static public int ToValidInt(object n) {
      if(n==null) return 0;
      if(n.GetType() == typeof(string)) {
            try {
                  return int.Parse((string)n);
            }
            catch( Exception ) {
                  return 0;
            }
      }
      if(n.GetType() == typeof(int)) {
            return (int)n;
      }
      if(n.GetType() == typeof(decimal)) {
            return decimal.ToInt32((decimal)n);
      }
      return 0;
}

posted @ Wednesday, March 26, 2008 12:23 PM | Feedback (0)