本文是備份,原文請看我的網站:  [C#]以前在VB語法裡面用慣的 IsNumeric,在C#不見了??

網址  http://www.dotblogs.com.tw/mis2000lab/archive/2008/10/13/csharp_vb_isnumeric.aspx

 

 

 

今天早上剛剛在MSDN討論區回答的問題,http://forums.microsoft.com/MSDN-CHT/ShowPost.aspx?PostID=3985918&SiteID=14

 

的確,用慣VB語法的朋友(例如我自己)剛剛轉換到 C#語法,會有很多不適應。

  • IsNumeric()用來判別是否為數字的函數,不見了?
  • VB語法的 CInt()與 CDtae()不能用?   要改成 (int) 與(DateTime)強制轉換資料型別....等等。
  • VB語法的 FormateDate也消失了.....等等。

因為C#是根本沒有任何函數,只能叫用.NET Framewrok裡的類別

===============================================

使用C#的朋友,您把下面這個函數放在程式碼最後,

就可以使用 IsNumeric()了

 

微軟出品的,用起來應該有信心

 

    // IsNumeric Function
    // 資料來源:
http://support.microsoft.com/kb/329488/zh-tw
    static bool IsNumeric(object Expression)
    {
        // Variable to collect the Return value of the TryParse method.
        bool isNum;

        // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
        double retNum;

        // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
        // The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
        isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);


        return isNum;
    }

 

.....................................................................................................................................................................................
ASP.NET專題實務ASP.NET專題實務(文魁出版 / P8187)

2008/八月份 天瓏書局排行榜前十名

.............................................................................................................. 寄信給我    mis2000lab (at) 雅虎.com.台灣 ........

arrow
arrow
    全站熱搜

    MIS2000 Lab 發表在 痞客邦 留言(0) 人氣()