1
Decimal Basic, Da, HRA, CCA, convey, splall, deduction1, deduction2, deduction3, deduction4, deduction5;
Decimal.TryParse(txtBasicSalary.Text, out Basic);
Decimal.TryParse(txtDA.Text, out Da);
Decimal.TryParse(txtHRA.Text, out HRA);
Decimal.TryParse(txtCCA.Text, out CCA);
Decimal.TryParse(txtConvey.Text, out Convey);
Decimal.TryParse(txtSplAll.Text, out splall);
Decimal.TryParse(txtdeduction1.Text, out deduction1);
Decimal.TryParse(txtdeduction2.Text, out deduction2);
Decimal.TryParse(txtdeduction3.Text, out deduction3);
Decimal.TryParse(txtdeduction3.Text, out deduction4);
Decimal.TryParse(txtdeduction5.Text, out deduction5);
drTmp["empl_Basic"] = Basic;
drTmp["empl_DA"] = Da;
drTmp["empl_HRA"] = HRA;
drTmp["empl_CCA"] = CCA;
drTmp["empl_Convey"] = convey;
drTmp["empl_Splall"] = splall;
drTmp["empl_Deduction1"] = deduction1;
drTmp["empl_Deduction2"] = deduction2;
drTmp["empl_Deduction3"] = deduction3;
drTmp["empl_Deduction4"] = deduction4;
drTmp["empl_Deduction5"] = deduction5;

上記のコードを 10 進変換に使用しています。すべてのテキスト ボックスで、複数の変数を使用してその変数を渡していますが、その代わりにすべてに同じ変数を使用できますか?

4

3 に答える 3

5

ユーティリティメソッドを書くことができます:

static decimal TryParse(
  string value, decimal @default = 0M)
{
    decimal ret;
    return decimal.TryParse(value, out ret) ? ret : @default;
}

そしてそれを使用します:

drTmp["empl_Basic"] = TryParse(txtBasicSalary.Text);
drTmp["empl_DA"] = TryParse(txtDA.Text);
drTmp["empl_HRA"] = TryParse(txtHRA.Text);

以下の使用法では、無効なデータをゼロ以外で処理することもできます。

drTmp["empl_HRA"] = TryParse(txtHRA.Text, 6.5M);
于 2013-03-07T11:45:23.653 に答える
1

必要に応じて、拡張メソッドなどのメソッドを使用できます。

public static Decimal? TryGetDecimal(this string item)
{
    Decimal d;
    bool success = Decimal.TryParse(item, out d);
    return success ? (Decimal?)d : (Decimal?)null;
}

明示的な変数宣言はまったく必要ありません。戻り値を使用するだけです。

drTmp["empl_Basic"] = txtBasicSalary.Text.TryGetDecimal() ?? 0;
drTmp["empl_DA"] = txtDA.Text.TryGetDecimal() ?? 0;
于 2013-03-07T11:47:46.920 に答える
0

はい、できます。私はかつてあなたの問題に遭遇し、Textbox コントロールの拡張機能を実装しました。これは、Decimal、int、値型であれば何でも変換できるジェネリック メソッドです。

public static T? GetTextOrNullStruct<T>(this TextBox txt, GlobalSist.Common.Utils.ParserCondition? parserCondition, bool throwOnValidation)
    where T : struct, IComparable<T>
{
    return GlobalSist.Common.Utils.Parsers.ConvertStruct<T>(
        GetTextOrNull(txt), parserCondition, throwOnValidation);
}


public static T? ConvertStruct<T>(IConvertible value, ParserCondition? parserCondition, bool throwOnValidation)
    where T : struct, IComparable<T>
{
    try
    {
        if ((value == null) ||
            (value is string && string.IsNullOrEmpty((string)value)))
        {
            if (throwOnValidation)
                throw new ArgumentNullException("value");
            else
                return null;
        }
        return Parsers.Convert<T>(value, parserCondition, true);
    }
    catch (ArgumentOutOfRangeException)
    {
        if (throwOnValidation)
            throw;
        else
            return null;
    }
}

そして、ここに文字列に変換するメソッドがあります

public static T Convert<T>(IConvertible value, ParserCondition? parserCondition, bool throwOnValidation)
            where T : IComparable<T>
        {
            T convertedValue;
            try
            {
                convertedValue = (T)value.ToType(typeof(T), null);
            }
            catch (Exception ex)
            {
                if (throwOnValidation)
                    throw new ArgumentOutOfRangeException(ex.Message, ex);
                else
                    return default(T);
            }
            return ValidateParserCondition<T>(convertedValue, parserCondition, throwOnValidation);
        }

編集:最後に呼び出されたメソッドを貼り付けるのを忘れました


private static T ValidateParserCondition<T>(T value, ParserCondition? parserCondition, bool throwOnValidation)
    where T : IComparable<T>
{
    if (parserCondition == null)
        return value;
    else
    {
        int comparingToZero = value.CompareTo(default(T));
        switch (parserCondition.Value)
        {
            case ParserCondition.GreaterOrEqualToZero:
                if (comparingToZero >= 0)
                    return value;
                break;
            case ParserCondition.GreaterThanZero:
                if (comparingToZero > 0)
                    return value;
                break;
            case ParserCondition.LessOrEqualToZero:
                if (comparingToZero <= 0)
                    return value;
                break;
            case ParserCondition.LessThanZero:
                if (comparingToZero < 0)
                    return value;
                break;
            default:
                throw new NotImplementedException("ParserCondition at ValidateParserCondition");
        }
        if (throwOnValidation)
            throw new ArgumentOutOfRangeException(
                string.Format("value {0} not in accordance with ParserCondition {1}",
                value, parserCondition.Value.ToString()));
        else
            return default(T);
    }

}

そして列挙宣言は次のとおりです。

public enum ParserCondition
{
    /// <summary>
    /// &gt;=0
    /// </summary>
    GreaterOrEqualToZero,
    /// <summary>
    /// &gt;0
    /// </summary>
    GreaterThanZero,
    /// <summary>
    /// &lt;=0
    /// </summary>
    LessOrEqualToZero,
    /// <summary>
    /// &lt;0
    /// </summary>
    LessThanZero,

}
于 2013-03-07T11:46:33.840 に答える