Posts
231
Comments
64
Trackbacks
4
February 2008 Entries
Kleine Funktion: ToValidDecimal

Hier eine kleine Funktion die einen übergebenen Wert in einen Decimal-Wert umwandelt:

static public decimal ToValidDecimal(object n, NumberFormatInfo nfi ) {

      if(n==null) return 0;

      if(n.GetType() == typeof(decimal)) return (decimal)n;
      if(n.GetType() == typeof(int)) return (decimal)n;
      if(n.GetType() == typeof(long)) return (decimal)n;
      if(n.GetType() == typeof(string)) {

            try {
                  return decimal.Parse( (string)n, nfi );
            }
            catch( Exception ) {
                  return (decimal)0;
            }

      }

      return (decimal)0;
}

posted @ Thursday, February 28, 2008 9:30 AM | Feedback (0)
Kleine Funktion: ToValidBool

Hier eine kleine Funktion welche überprüft, ob ein übergebenes Objekt ein Bool-Wert ist und diesen in einen entsprechenden boolschen Wert umwandelt: 

static public bool ToValidBool(object n) {

     if(n==null) return false
     if(n.GetType() == typeof(bool)) { 
          return (bool)n; 
     }

     if(n.GetType() == typeof(string)) { 
          // A given String return "true", if it's not empty 
          string s = ((string)n).Trim().ToLower();

          if( s=="1" || s=="-1" || 
             Limit(s,4)=="true" || 
             Limit(s,3)=="yes" || 
             Limit(s,2)=="ja" || 
             Limit(s,2)=="on" ) return true
     }

     if(n.GetType() == typeof(int)) { 
        if((int)n > 0 ) return true
     }

     return false;
}

posted @ Tuesday, February 19, 2008 9:18 AM | Feedback (0)
Kleine Funktion für ein gültiges Datum

Hier eine kleine Funktion welche überprüft, ob ein übergebenes Objekt ein Datum ist und diesen Wert als Datum zurückgibt: 

            static public DateTime ToValidDateTime(object n) {

                  if(n==null) return DateTime.MinValue;
                  if(n.GetType() == typeof(DateTime)) {
                        if((DateTime)n < DateTime.MinValue) {
                             return DateTime.MinValue;
                        } else {
                             return (DateTime)n;
                        }
                  }

                  try {
                        if(n.GetType() == typeof(string)) {
                             return DateTime.Parse( (string)n, sFormat );
                        }
                  }
                  catch( FormatException ) {
                  }

                  return DateTime.MinValue;
            }

posted @ Friday, February 01, 2008 9:06 AM | Feedback (0)