2013-03-07 55 views
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; 

我使用上述十進制皈依代碼,所有的文本框我使用多個變量,並傳遞一個變量,而不是,我可以使用相同的變量爲所有?如何使用單一變量,而不是在C#.NET使用多變量

+1

您似乎忽略了'TryParse'的返回值,因此爲無效數據分配了'0M';那是故意的嗎? – 2013-03-07 11:43:03

+0

我想只使用一個變量,並用於所有 – user1918410 2013-03-07 11:43:40

+0

我使用上述代碼在薪酬管理過程中爲emp sal形成,在上面我採取multople變量,我通過每個文本boxess – user1918410 2013-03-07 11:49:14

回答

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

} 
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; 
相關問題