So today I came across a bug in our new system, which is written in C#.NET 4.0.

It was an upgrade from an old VB6 app, and it happened to be the way that Val() was used in VB6.

Since there is no direct equivalant, the internet didn’t provide me fast enough with a good enough method and it’s behavior is kinda odd, here’s my best approiximation in C#:

public static Double Val(string value)
{
    String result = String.Empty;
    foreach (char c in value)
    {
        if (Char.IsNumber(c) || (c.Equals('.') && result.Count(x => x.Equals('.')) == 0))
            result += c;
        else if (!c.Equals(' '))
            return String.IsNullOrEmpty(result) ? 0 : Convert.ToDouble(result);
    }
    return String.IsNullOrEmpty(result) ? 0 : Convert.ToDouble(result);
}

Works great!
(my initial Regex method was much faster, but lacked the accuracy I wanted)

One thought on “VB6 Val() equivalent in C#

  1. We are a gaggle of volunteers and starting a new scheme in our community. Your website provided us with valuable information to work on. You’ve done an impressive activity and our entire community might be grateful to you.|

Leave a Reply

Your email address will not be published. Required fields are marked *