in

dotnet.org.za

South African .NET Developer Portal

The Bumpy Blog

Bumpy's Bedside Story

July 2004 - Posts

  • Gang of Four Patterns

    I found a great site that explains the Gang of Four Patterns in a very nice way with .NET C# examples.  Very nice

  • IsNumeric in C#, WHY NOT?

    This is something that has bothered me for a long while now.  Why doesn't C# have a IsNumeric(string num) function like VB.NET?
    I have used every kind of IsNumeric code you van think of...  This is what I started with.

    public bool IsNumeric(string s)
    {
          try
          {
                Int32.Parse(s);
          }
          catch
          {
                
    return false;
          }
          return true;
    }

    But we all no the unspoken rule, NEVER use try, catch for functionality...  So I tried this.

    internal static bool IsNumeric(string numberString)
    {
          char [] ca = numberString.ToCharArray();
          for (int i = 0; i < ca.Length;i++)
          { 
                if (ca[i] > 57 || ca[i] < 48)
                return false;
          }
          return true;
    }

    Then I found char.IsNumber:  (Why do they have a char.IsNumber, but no string.IsNumeric???)

    internal static bool IsNumeric(string numberString)
    {
          char [] ca = numberString.ToCharArray();
          for (int i = 0; i < ca.Length;i++)
          { 
                if (!char.IsNumber(ca[i]))
                return false;
          }
          return true;
    }

    With the help of some friend (Thanks Dawid), I finally got this.  What do you think?

    public static bool IsNumeric(object Expression)
    {
          bool isNum;
          double retNum;
          isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any,System.Globalization.NumberFormatInfo.InvariantInfo, out retNum );
          return isNum;
    }

    Luckily, as far as I heard, Whidbey has a IsNumeric build in.

  • New Business Venture

    I have a favour to ask!  Does anyone of you know of people that are looking for new jobs?  Suzette will be starting a Recruitment Agency as of the 1st of August.  New venture and of course she needs CV's or at least people that will be looking for jobs.  If you know of anyone, please mail the details.
Powered by Community Server (Commercial Edition), by Telligent Systems