Previous | Index | Next 

[BUG] Testing Err after invoking Format6 method

Format6 method defined in the VB6Library maps over the VB.NET Format method (defined in the Microsoft.VisualBasic.Compatibility.VB6.Format namespace), which internally clears the Err object (and possibly sets it if the Format function itself throws an exception). This behavior is different from the VB6 Format method, which doesn’t affect the value of the Err object.

As a result, you cannot test the Err object after invoking the Format6 method, because the Err object is likely to be cleared at that point. The obvious workaround is that you save the value of the Err.Number property (and possibly Err.Description) before invoking the Format6 method:

' this VB6 code doesn’t work well after migrating to .NET
result = EvalSomething()
MsgBox "Result = " & Format("###.00", result)
If Err.Number <> 0 Then MsgBox Err.Description
‘ this VB6 code works well even after the migration to .NET
result = EvalSomething()
Dim errNumber As Integer, errDescription As String
errNumber = Err.Number
errDescription = Err.Description
MsgBox "Result = " & Format("###.00", result)
If errNumber <> 0 Then MsgBox errDescription

 

Previous | Index | Next