当T为double或Int时,如何在通用方法中使用< T> .TryParse [英] how to use <T>.TryParse in a generic Method while T is either double or Int
问题描述
在我的一个项目中,我正在使用以下两种方法. 1. GetDoubleValue和2. GetIntValue. GetDoubleValue使用double.TryParse到参数str字符串,如果失败则返回0,而GetIntValue尝试int.TryParse到参数str字符串,如果失败则返回0.我想要的是将这两种方法组合为一个通用方法,该方法与字符串str一起也接收参数T,因此,如果我要使用GetDoubleValue方法,则可以对参数T使用double;如果要使用GetIntValue方法,则可以使用参数T的Int
in one of my projects I'm using following two methods. 1. GetDoubleValue and 2. GetIntValue. GetDoubleValue uses double.TryParse to parameter str string and returns 0 if it fails while GetIntValue tries int.TryParse to parameter str string and returns 0 if it fails. What I want is to combine these two methods into one generic method that alongwith string str receives parameter T as well so that if I want to use GetDoubleValue Method I can use double for the parameter T and if I want to use GetIntValue Method I can use Int for the parameter T
public double GetDoubleValue(string str)
{
double d;
double.TryParse(str, out d);
return d;
}
public int GetIntValue(string str)
{
int i;
int.TryParse(str, out i);
return i;
}
注意:我已经尝试过类似的事情;
Note: I have tried something like this;
private T GetDoubleOrIntValue<T>(string str) where T : struct
{
T t;
t.TryParse(str, out t);
return t;
}
编辑
在我的数据库中,具有数字数据类型的不同表中有30多个列.如果用户未在文本框中输入任何内容,即他将所有或部分文本框留空,我想在每一列中插入0.如果不使用GetIntValue方法,则必须使用该方法主体30次以上.这就是为什么我通过方法来做到这一点.例如,我正在写三十多个示例中的三个
In my database I have more than 30 columns in differenct tables having numeric datatype. I want to insert 0 in each column if user does not type anything in the textbox i.e he leaves all or some of the textboxes empty. If I don't use the GetIntValue method I will have to use the method body more than 30 times. that is why I am doing this through method approach. I'm writing three of more than thirty examples for instance
cmd.Parameters.Add("@AddmissionFee", SqlDbType.Decimal).Value = GetIntValue(tbadmissionfee.Text);
cmd.Parameters.Add("@ComputerFee", SqlDbType.Decimal).Value = GetIntValue(tbcomputerfee.Text);
cmd.Parameters.Add("@NotesCharges", SqlDbType.Decimal).Value = GetDoubleValue(tbnotescharges.Text);
我想将上述两种方法结合起来,因为今天我有两种这样的方法,如果结合起来使用,它们在编程方面不会带来任何更好的改善,但是明天,我可能会拥有数十种这样的方法,可以将它们更好地结合在一起通用方法.例如,我可能有GetInt32Value,GetShortValue等.希望现在可以清除为什么要这样做了?
I want to combine the aforesaid two methods because today I'm having two methods like this which if combined will not give any better improvement in the programming but tomorrow I may have tens of methods like this that will better be combined into one generic method. e.g I may have GetInt32Value, GetShortValue etc. hope it is now cleared why I want this???
推荐答案
您可以执行以下操作:
public static T GetDoubleOrIntValue<T>(this string str) where T : IConvertible
{
var thisType = default(T);
var typeCode = thisType.GetTypeCode();
if (typeCode == TypeCode.Double)
{
double d;
double.TryParse(str, out d);
return (T)Convert.ChangeType(d,typeCode) ;
}
else if (typeCode == TypeCode.Int32)
{
int i;
int.TryParse(str, out i);
return (T)Convert.ChangeType(i, typeCode);
}
return thisType;
}
然后当您调用它时:
string d = "1.1";
string i = "3";
double doubleValue = d.GetDoubleOrIntValue<double>();
int intValue = i.GetDoubleOrIntValue<int>();
但是整个事情对我来说似乎很愚蠢.
But the whole thing seems kinda silly to me.
使用Convert.ChangeType ...看到其他人,它提供了通用的返回类型.
Saw someone else using Convert.ChangeType...which provides for a generic return type.
这篇关于当T为double或Int时,如何在通用方法中使用< T> .TryParse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!