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;

I am using above code for decimal convertion, for all text boxes I am using multiple variables and passing that variables, instead of that can I use same variable for all?

5
  • 1
    You seem to be ignoring the return value of TryParse, thus assigning 0M for invalid data; is that intentional? Commented Mar 7, 2013 at 11:43
  • i want use only one variable and use that for all Commented Mar 7, 2013 at 11:43
  • i used that above code in pay roll manage ment process for emp sal in formation,in that above i am taking multople variables and i am passing that every text boxess Commented Mar 7, 2013 at 11:49
  • instead of that can i take one variable i cant apply for all Commented Mar 7, 2013 at 11:50
  • I look forward to receiving $0 on my paycheque because someone in HR typo'd the txtBasicSalary. Commented Mar 7, 2013 at 11:57

3 Answers 3

5

You could write a utility method:

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

and use that:

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

the usage below also allows for non-zero handling of invalid data:

drTmp["empl_HRA"] = TryParse(txtHRA.Text, 6.5M);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a method if you want, for example as extension method:

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

Now you don't need explicit variable declarations at all, just use the return value:

drTmp["empl_Basic"] = txtBasicSalary.Text.TryGetDecimal() ?? 0;
drTmp["empl_DA"] = txtDA.Text.TryGetDecimal() ?? 0;

Comments

0

Yes, you can. I once came across your issue and implemented an extension for the Textbox control. It's a generic method that can convert do Decimal, int, anything that is a value type.

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;
    }
}

And here it is the method that converts to a string

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);
        }

edit: Forgot to paste the last called method


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);
    }

}

And the enum declaration is:

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.