Posts
231
Comments
64
Trackbacks
4
Friday, April 04, 2008
Kleine Funktion: ToValidByte

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

static public byte ToValidByte(object n) {
      if(n==null) return 0;
      if(n.GetType() == typeof(string)) {
            try {
                  return byte.Parse((string)n);
            }
            catch( Exception ) {
                  return 0;
            }
      }
      if(n.GetType() == typeof(byte)) {
            return (byte)n;
      }
      if(n.GetType() == typeof(decimal)) {
            return decimal.ToByte((decimal)n);
      }
      return 0;
}

posted @ Friday, April 04, 2008 12:30 PM | Feedback (0)