首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >紧凑框架上的C# Double.TryParse等价物

紧凑框架上的C# Double.TryParse等价物
EN

Stack Overflow用户
提问于 2014-07-29 19:32:25
回答 2查看 3K关注 0票数 2

我们希望在Win CE5.0供电的设备上实现一个货币输入仅文本框(###.00)。该应用程序是使用.NET Compact Framework3.5 (C#)开发的。

我被建议了以下的解决方案:

代码语言:javascript
运行
复制
    private void textbox1_KeyDown(object sender, KeyEventArgs e)
    {
        double amount = 0.0d;
        if (double.TryParse(txtbox1.Text, NumberStyles.Currency, null, out amount))
        {
            textbox.Text = amount.ToString("C");
        }
    }

(框架压缩版不支持Decimal.TryParse)?

EN

回答 2

Stack Overflow用户

发布于 2014-07-29 19:39:41

压缩框架不支持TryParse。

您可以将其替换为:

代码语言:javascript
运行
复制
    private void textbox1_KeyDown(object sender, KeyEventArgs e)
    {
        double amount = 0.0d;

        try
        {
            amount = Convert.ToDouble(txtbox1.Text);
            textbox.Text = amount.ToString("C");
        }
        catch
        {

        }          
    }

或者参考这个博客,了解Compact Framework的TryParse的实现:https://web.archive.org/web/20160606182643/http://www.yortondotnet.com/2009/11/tryparse-for-compact-framework.html

票数 1
EN

Stack Overflow用户

发布于 2016-03-30 16:39:03

用于压缩框架的TryParse

代码语言:javascript
运行
复制
/// <summary>
/// Contains methods to assist with parsing one value into another.
/// </summary>
public static class ParseAssistant
{
  #region TryParse Overloads
  /// <summary>
  /// Attempts to parse the string provided into an integer value. 
  /// </summary>
  /// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
  /// <param name="s">The string to attempt to parse.</param>
  /// <param name="result">The result of the parsed string, or zero if parsing failed.</param>

  /// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
  public static bool TryParse(string s, out int result)
  {
    bool retVal = false;
#if WindowsCE
        try
        {
            result = Convert.ToInt32(s);
            retVal = true;
        }
        catch (FormatException) { result = 0; }
        catch (InvalidCastException) { result = 0; }
#else
    retVal = int.TryParse(s, out result);
#endif
    return retVal;
  }
  /// <summary>
  /// Attempts to parse the string provided into a byte value. 
  /// </summary>
  /// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
  /// <param name="s">The string to attempt to parse.</param>
  /// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
  /// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
  public static bool TryParse(string s, out byte result)
  {
    bool retVal = false;
#if WindowsCE
        try
        {
            result = Convert.ToByte(s);
            retVal = true;
        }
        catch (FormatException) { result = 0; }
        catch (InvalidCastException) { result = 0; }
#else
    retVal = byte.TryParse(s, out result);
#endif
    return retVal;
  }
  /// <summary>
  /// Attempts to parse the string provided into an Int16 value. 
  /// </summary>
  /// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
  /// <param name="s">The string to attempt to parse.</param>
  /// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
  /// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
  public static bool TryParse(string s, out Int16 result)
  {
    bool retVal = false;
#if WindowsCE
        try
        {
            result = Convert.ToInt16(s);
            retVal = true;
        }
        catch (FormatException) { result = 0; }
        catch (InvalidCastException) { result = 0; }
#else
    retVal = Int16.TryParse(s, out result);
#endif
    return retVal;
  }
  /// <summary>
  /// Attempts to parse the string provided into an Int64 value. 
  /// </summary>
  /// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
  /// <param name="s">The string to attempt to parse.</param>
  /// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
  /// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
  public static bool TryParse(string s, out Int64 result)
  {
    bool retVal = false;
#if WindowsCE
        try
        {
            result = Convert.ToInt64(s);
            retVal = true;
        }
        catch (FormatException) { result = 0; }
        catch (InvalidCastException) { result = 0; }
#else
    retVal = Int64.TryParse(s, out result);
#endif
    return retVal;
  }
  /// <summary>
  /// Attempts to parse the string provided into a decimal value. 
  /// </summary>
  /// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
  /// <param name="s">The string to attempt to parse.</param>
  /// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
  /// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
  public static bool TryParse(string s, out decimal result)
  {
    bool retVal = false;
#if WindowsCE
        try
        {
            result = Convert.ToDecimal(s);
            retVal = true;
        }
        catch (FormatException) { result = 0; }
        catch (InvalidCastException) { result = 0; }
#else
    retVal = decimal.TryParse(s, out result);
#endif
    return retVal;
  }
  /// <summary>
  /// Attempts to parse the string provided into a float value. 
  /// </summary>
  /// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
  /// <param name="s">The string to attempt to parse.</param>
  /// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
  /// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
  public static bool TryParse(string s, out float result)
  {
    bool retVal = false;
#if WindowsCE
        try
        {
            result = (float)Convert.ToDecimal(s);
            retVal = true;
        }
        catch (FormatException) { result = 0; }
        catch (InvalidCastException) { result = 0; }
#else
    retVal = float.TryParse(s, out result);
#endif
    return retVal;
  }
  /// <summary>
  /// Attempts to parse the string provided into a double value. 
  /// </summary>
  /// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
  /// <param name="s">The string to attempt to parse.</param>
  /// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
  /// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
  public static bool TryParse(string s, out double result)
  {
    bool retVal = false;
#if WindowsCE
        try
        {
            result = Convert.ToDouble(s);
            retVal = true;
        }
        catch (FormatException) { result = 0; }
        catch (InvalidCastException) { result = 0; }
#else
    retVal = double.TryParse(s, out result);
#endif
    return retVal;
  }
  /// <summary>
  /// Attempts to parse the string provided into an sbyte value. 
  /// </summary>
  /// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
  /// <param name="s">The string to attempt to parse.</param>
  /// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
  /// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
  public static bool TryParse(string s, out sbyte result)
  {
    bool retVal = false;
#if WindowsCE
        try
        {
            result = (sbyte)Convert.ToInt32(s);
            retVal = true;
        }
        catch (FormatException) { result = 0; }
        catch (InvalidCastException) { result = 0; }
#else
    retVal = sbyte.TryParse(s, out result);
#endif
    return retVal;
  }
  /// <summary>
  /// Attempts to parse the string provided into a uint value. 
  /// </summary>
  /// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
  /// <param name="s">The string to attempt to parse.</param>
  /// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
  /// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
  public static bool TryParse(string s, out uint result)
  {
    bool retVal = false;
#if WindowsCE
        try
        {
            result = (uint)Convert.ToUInt64(s);
            retVal = true;
        }
        catch (FormatException) { result = 0; }
        catch (InvalidCastException) { result = 0; }
#else
    retVal = uint.TryParse(s, out result);
#endif
    return retVal;
  }
  /// <summary>
  /// Attempts to parse the string provided into a ulong value. 
  /// </summary>
  /// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
  /// <param name="s">The string to attempt to parse.</param>
  /// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
  /// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
  public static bool TryParse(string s, out ulong result)
  {
    bool retVal = false;
#if WindowsCE
        try
        {
            result = (ulong)Convert.ToUInt64(s);
            retVal = true;
        }
        catch (FormatException) { result = 0; }
        catch (InvalidCastException) { result = 0; }
#else
    retVal = ulong.TryParse(s, out result);
#endif
    return retVal;
  }
  /// <summary>
  /// Attempts to parse the string provided into a ushort value. 
  /// </summary>
  /// <remarks>Returns 0 in the result parameter if the parse fails.</remarks>
  /// <param name="s">The string to attempt to parse.</param>
  /// <param name="result">The result of the parsed string, or zero if parsing failed.</param>
  /// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
  public static bool TryParse(string s, out ushort result)
  {
    bool retVal = false;
#if WindowsCE
        try
        {
            result = (ushort)Convert.ToUInt64(s);
            retVal = true;
        }
        catch (FormatException) { result = 0; }
        catch (InvalidCastException) { result = 0; }
#else
    retVal = ushort.TryParse(s, out result);
#endif
    return retVal;
  }
  /// <summary>
  /// Attempts to parse the string provided into an <see cref="System.DateTime"/> value. 
  /// </summary>
  /// <remarks>Returns <see cref="System.DateTime.MinValue"/> in the result parameter if the parse fails.</remarks>
  /// <param name="s">The string to attempt to parse.</param>
  /// <param name="result">The result of the parsed string, or <see cref="System.DateTime.MinValue"/> if parsing failed.</param>
  /// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
  public static bool TryParse(string s, out DateTime result)
  {
    bool retVal = false;
#if WindowsCE
        try
        {
            result = Convert.ToDateTime(s);
            retVal = true;
        }
        catch (FormatException) { result = DateTime.MinValue; }
        catch (InvalidCastException) { result = DateTime.MinValue; }
#else
    retVal = DateTime.TryParse(s, out result);
#endif
    return retVal;
  }
  /// <summary>
  /// Attempts to parse the string provided into an integer value. 
  /// </summary>
  /// <remarks>Returns false in the result parameter if the parse fails.</remarks>
  /// <param name="s">The string to attempt to parse.</param>
  /// <param name="result">The result of the parsed string, or false if parsing failed.</param>
  /// <returns>A boolean value indicating whether or not the parse succeeded.</returns>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
  public static bool TryParse(string s, out bool result)
  {
    bool retVal = false;
#if WindowsCE
        try
        {
            result = Convert.ToBoolean(s);
            retVal = true;
        }
        catch (FormatException) { result = false; }
        catch (InvalidCastException) { result = false; }
#else
    retVal = bool.TryParse(s, out result);
#endif
    return retVal;
  }
  #endregion
}

https://web.archive.org/web/20160606182643/http://www.yortondotnet.com/2009/11/tryparse-for-compact-framework.html

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25014283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档