Is it possible to convert vb6 “Val()” to c#?

I am currently converting vb and vb.net to c# but have an issue. I would strongly like not to use the visualbasic dlls in the converted code and have been doing this fine so far.

But this code

Dim x as Double    'this was error saying it was integer
x = Val("1 23 45 x 6")  ''#x is  12345
x = Val("1..23")    ''#x is 1.23
x = Val("1 1,,,,,2,2..3") ''#x is 1122.3

Does not work the same as vb6 even with using "Val" from the visualbasic.conversion.dll Is there anyone that has solved this to work the same? A c# solution would be best.

Answers


Check out this site: http://www.dreamincode.net/forums/topic/36064-val-replacement/ where others give an example how to implement your own Val() function.


I know nothing of this VisualBasic.Conversion.dll (and neither does google), but the Microsoft.VisualBasic namespace (in Microsoft.VisualBasic.dll) is part of the core framework and perfectly fine and acceptable to use from C#. There are other nice gems in there as well (ie TextFieldParser), but this should have the exact Val() implementation you need.

If this is the library you've already tried and it doesn't seem right, then I'd go take another look at the unit tests on it.

Outside of this, the accepted ways in c# for converting strings to integers are int.Parse(), int.TryParse(), and Convert.ToInt32(). But, like it or not, the Val() function from the Microsoft.VisualBasic.dll library is the closest match you're going to find for your code's existing behavior.


None of the above seemed to satisfy me, so I wrote the below:

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

So far, all my tests have returned the same data. i.e. "0 1 5.2123 123.123. 1 a" returns 15.21233123 i.e. " 1 5.2123 123a" returns 15.21233123 i.e. "a1 5.2123 123.123. 1 a" returns 0 i.e. "" returns 0


Need Your Help

Resize NSArray programmatically

objective-c resize nsarray category

I have been trying to figure out if it is possible to programmatically resize an NSArray, using code similar to this:

Ruby array methods for locating based on an object's attribute?

ruby arrays syntax

Say I have a Ruby class, Flight. Flight has an attr_accessor :key on it. If there's an array of instances of this class: flights = [flight1, flight2, flight3], I have a "target key", say "2jf345", ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.