vb.netvb6vb6-migration

How to migrate string format function with "!@@@" from vb6.0 to vb.net?


I'm migrating source from vb6.0 to vb.net and am struggling with this format function:

VB6.Format(text, "!@@@@@@@@@")

VB6.Format(text, "00000")

I don't understand the meaning of "!@@@@@@@@@" and "00000", and how to do the equivalent in VB.Net. Thanks


Solution

  • This:

    VB6.Format(text, "!@@@@@@@@@")
    

    indicates that the specified text should be left-aligned within a nine-character string. Using standard .NET functionality, that would look like this:

    String.Format("{0,-9}", text)
    

    or, using the newer string interpolation, like this:

    $"{text,-9}"
    

    The second on is a little bit trickier. It's indicating that the specified text should be formatted as a number, zero-padded to five digits. In .NET, only actual numbers can be formatted as numbers. Strings containing digit characters are not numbers. You could convert the String to a number and then format it:

    String.Format("{0:00000}", CInt(text))
    

    or:

    String.Format("{0:D5}", CInt(text))
    

    If you were going to do that then it's simpler to just call ToString on the number:

    CInt(text).ToString("D5")
    

    If you don't want to do the conversion then you can pad the String explicitly instead:

    text.PadLeft(5, "0"c)