Previous | Index | Next 

[HOWTO] Convert byte-oriented string functions

VB.NET doesn’t support byte-oriented string functions, such as LeftB, RightB, and MidB. VB Migration Partner converts these methods into calls to the special LeftB6, RightB6, and MidB6 methods, defined the support library. These replacement methods mimic their VB6 counterparts, but in most cases they fail to perfectly reproduce the original VB6 behavior. For this reason, the methods in the support library are marked as obsolete and generate both a migration message and a compiler warning.

To help users that work with non-Latin alphabets, starting with 1.11 version, the support library exposes the following helper methods that approximate the VB6 behavior a bit better: EncodeStr6, EncodeLeftB6, EncodeRightB6, EncodeMidB6 and EncodeInputB6

        Dim s As String
        s = EncodeLeftB6("abcde", 5)

The new methods solve one of the problems inherent to the standard XxxxB6 function by supporting an additional, optional Boolean parameter. If this parameter is passed True, then the method returns a Byte array rather than a string:

        Dim bytes() As Byte
        s = EncodeLeftB6("abcde", 5, True)

You can easily replace all the occurrences of standard XxxxB6 methods into the new EncodeXxxxB6 methods by means of a PostProcess pragma in the original VB6 code:

        '## project:PostProcess "\b(Left|Right|Mid|Input)B6\(", "Encode$0", True
        '## project:PostProcess "\bStrConv6\(", "EncodeStr6("

The pragmas are different if you are converting to C#:

'## project:PostProcess "\bVB6Helpers.(?Left|Right|Mid|Input)B\(", 
"VB6Helpers.Encode${fn}B(", True
        '## project:PostProcess "\bVB6Helpers.StrConv\(", "VB6Helpers.EncodeStr("

If you have used the new EncodeXxxx methods in your VB6 code, you don’t need an explicit conversion from String to Byte array any longer, therefore you can drop occurrences of the StringToByteArray6 method and append True at the end of the argument list. The following pragma can do that automatically:

        '## PostProcess "\bStringToByteArray6\((?<instruction>Encode(LeftB6|RightB6|MidB6|InputB6)
           \(.+?)\)\)", "${instruction}, True)", True, False

(Note that previous pragma fails if the EncodeXxxx6 method has one or more parameters that contain parenthesis.) Here’s the version needed when converting to C#

'## PostProcess 
"\bVB6Helpers.StringToByteArray\((?VB6Helpers.Encode(LeftB|RightB|MidB|InputB)\(.+?)\)\)" , 
"${instruction}, true)", True, False

The new methods are defined also in the VisualBasic6_Support.bas module, therefore you can take a more granular decision about which byte-oriented methods are converted and use the new functionality.

Finally, a disclaimer is in order. Even with these improvements, the new methods can’t guaranteed that VB6 functionality is perfectly replicated; however they are an important step in the right direction.

 

Previous | Index | Next