【解決方法】5 つのテキストボックスの合計の計算


5 つのテキスト ボックスがあり、それらに入力された値を追加して、5 つの値すべての合計を計算したいと考えています。 ASP.net を使用しています。

これで私を導いてください!

解決策 1

ここから始めましょう:

C#
public Object Parse(String stringValue, Type typ)
{
    Type[] parseParams = new Type[] { typeof(String) };
    Object retVal = null;
    // This method should also check that the Parse method matches the type, but I'll leave that as an excercise
    MethodInfo mi = typ.GetMethod("Parse", parseParams);
    if (mi != null)
    {
        retVal = mi.Invoke(this, new Object[] { stringValue });
    }
    return retVal;
}

private D GetValue<D>(Textbox tb)
{
    D result = default(D);
    try
    {
        result = (D)Parse(tb.Text, typeof(D));
    }
    catch
    {
    }
    return result;
}

private T DoSum<T>(Textbox[] textboxes)
{
    T result = default(T);
    foreach(Textbox tb in textboxes)
    {
        result = Add<T>(result, GetValue<T>(tb));
    }
}

private L Add<L>(L result, L addend)
{
    L result = default(L);
    ...
    ...
    ...
    return result;
}

乾杯!

-やあ

コメント

タイトルとURLをコピーしました