General method for checking int, double. How to use gettype ()?

I am trying to write a validation method. For example: for double, it looks like this:

   protected bool ValidateLoopAttributes(string i_value, double i_threshold)
       {
        double result;
        if (!(double.TryParse(i_value, out result) && result >= i_threshold))
        {
            return false;
        }
        return true;
    }

Can this be written as:

     protected bool ValidateLoopAttributes<T>(string i_value, T i_threshold)

and then use something like

             T.GetType().TryParse() // how can i use here the type methods??

Does the switch / if statement use the only way to do this? For instance:

    If (T.GetType() is int) 
        Int32.TryParse(i_threshold)

Is there a more elegant way?

+3
source share
4 answers

Try the following:

static class Ext
{
    public static bool TryParse<T>(string s, out T value)
    {
        TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
        try
        {
            value = (T)converter.ConvertFromString(s);
            return true;
        }
        catch
        {
            value = default(T);
            return false;
        }
    }
    public static bool ValidateLoopAttributes<T>(string i_value, T i_threshold) 
           where T : IComparable
    {
        T outval;
        if (TryParse<T>(i_value, out outval))
            return outval.CompareTo(i_threshold) >= 0;
        else return false;
    }
}

My answer uses Mark Gravell's answer taken from here .
With this you can do

bool b1 = Ext.ValidateLoopAttributes<int>("5", 4);
bool b2 = Ext.ValidateLoopAttributes<double>("5.4", 5.5d);

If you find this useful, you can also use the extension method.

public static bool ValidateLoopAttributes<T>(this string i_value, T i_threshold) 
       where T : IComparable { }

which allows you to use

bool b1 = "5".ValidateLoopAttributes<int>(4);
bool b2 = "5.4".ValidateLoopAttributes<double>(5.5d);
+1
source
public static bool ValidateLoopAttributes<T>(string value, T threshold)
    where T : IComparable
{
    try
    {
        var parseMethod = typeof(T).GetMethod("Parse", new[] {typeof (string)});
        var result = (T) parseMethod.Invoke(null, new object[] {value});
        return result.CompareTo(threshold) < 0;
    }
    catch (Exception)
    {
        return false;
    }
}

Obviously, this only works for types with the static Parse method.

+1
source

- -. , ValidateLoopAttributes(value, 4), false. :

  • . . , ..
  • . . .
  • String , .
  • , .

. , double. , - . ( , , ), .

, - .

Foo foo = Parse(xml);
RunBusinessRules(foo); 
+1

You could try using something like this to check if it is an integer or not:

public static bool IsNumericValue(string val, System.Globalization.NumberStyles NumberStyle)
{
    double result;
    return Double.TryParse(val,NumberStyle,
        System.Globalization.CultureInfo.CurrentCulture,out result);
}

so on

 IsNumericValue("1.2", System.Globalization.NumberStyles.Integer) // FALSE 

and on

 IsNumericValue("12", System.Globalization.NumberStyles.Integer)  // TRUE

Note that in this example I used CurrectCulture, tailoring it to your needs if they are different.

0
source

All Articles