0

I recently took over a 170k line VB.net project and was instructed to bring it into C#. Not having the time for a full rewrite, I tested several conversion tools. I ended up using Tangible's Instant C#, which in all actuality did an incredible job. After a few hours of error fixes, it compiles and (mostly) works correctly.

With C# having no equivalent to VB's Val, the converter created a SimulateVal helper class with methods such as the following:

public static double Val(string expression)
{
    if (expression == null)
        return 0;

    //try the entire string, then progressively smaller
    //substrings to simulate the behavior of VB's 'Val',
    //which ignores trailing characters after a recognizable value:
    for (var size = expression.Length; size > 0; size--)
    {
        double testDouble;
        if (double.TryParse(expression.Substring(0, size), out testDouble))
            return testDouble;
    }

    //no value is recognized, so return 0:
    return 0;
}

Val was used throughout the VB program. Thus, these helper methods get called a number of times.

In searching for more information and other options, I've found this discussion, which features a response from Tangible showing this helper class. However, I've been unable to find much further discussion.

Will it be sufficient to leave this helper class in place and simply use the helper class? Would it be better to search for an alternative way of handling this in C#, or is this a light enough solution to leave in place?

Kiel
  • 408
  • 4
  • 13
  • A company has different goals from a pragmatic programmer. Who would simply keep using the [same method](https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.conversion.val%28v=vs.110%29.aspx) since it is available in any .NET Framework version. – Hans Passant Apr 01 '16 at 15:57
  • @HansPassant: Yes - referencing Microsoft.VisualBasic is a solution that will yield exactly equivalent results. However, many (you would call them non-pragmatic) feel that the conversion is incomplete if it still depends on the Microsoft.VisualBasic assembly. – Dave Doknjas Apr 01 '16 at 16:02
  • Thanks to both of you. I'm unsure if "complete conversion" is really necessary, so I'm tempted to use the Microsoft.VisualBasic reference. I'm rather green (dumb?) when it comes to performance issuess - does referencing have an impact on performance compared to a helper class? Is the idea of complete conversion why Tangible would make a helper class for something that C# has no direct equivalent to, rather than referencing VB? Lastly, I'd like to think I'm pragmatic, but really I just want to get the job done and do it as "right" as possible, but I'm still learning what "right" is. – Kiel Apr 01 '16 at 16:08
  • @Kiel: There is an option in the converter to continue using the original 'Val' method, but I think if you examine your 'Val' instances you might find that a simpler direct replacement may be appropriate. On the other hand, if you absolutely want to avoid any risk of your code depending on the rather strange 'Val' idiosyncrasies, then use the original method. – Dave Doknjas Apr 01 '16 at 16:10

1 Answers1

0

The helper class is intended to cover some strange cases that the original VB 'Val' covers. If your usage of 'Val' doesn't depend on the original VB idiosyncrasies, then you might be able to replace by a simple TryParse.

Just be aware, 'Val' does a lot more than a simple TryParse.

You could also examine the original VB 'Val' code to see exactly what it's doing - likely more than even the helper class is doing.

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28
  • Thanks @Dave. I'll look deeper, but I believe a number of the usages could be replaced with `TryParse`. The helper class uses `TryParse`, but also handles a variety of different data types. I'll look further into it and see what I can discover. Lastly, and I found [this](https://blogs.msdn.microsoft.com/nickmalik/2005/09/06/are-helper-classes-evil/) interesting, an old MSDN article discussing the evils of such classes: – Kiel Apr 01 '16 at 16:18
  • @Kiel: Remember that 'Val' has non-intuitive results - for example, "2@@@" has a value of 2 according to 'Val'. If you're not depending on this kind of craziness, then simple replacement with TryParse may work for you. – Dave Doknjas Apr 01 '16 at 16:29