I have to copy an algorithm from a legacy Visual Basic software and there is one thing about the code that I don't understand.
It seems the author has used some way to implicitly pass simple data types like integers by reference. The first time I thought it might be a bug in the original software, but after the third occurrence it looks intentional.
I wasn't able to run and debug the original (VB6 or prior) code. And in every simple code example where I tried to reproduce the behaviour (using VB.NET) it is always passed by value, unless ByRef is used explicitly.
I made an example:
Option Explicit
Module VBModule
Public Sub Calculate(value)
value = value + 1
End Sub
Function Starter()
Dim value%
Call Calculate(value)
If value > 0 Then
Console.WriteLine("Success")
End If
Console.WriteLine(value)
End Function
Sub Main()
Call Starter()
End Sub
End Module
The code depends on value
being changed by the Calculate
Sub to reach "Success". In the original project there are hundreds of lines of code that would be unreachable if this doesn't work.
So the question is, is there a way to have value
passed by reference without using ByRef while still using the integer type? Or is the code I'm trying to understand completely buggy?
Nowhere in the code base the author used an explicit ByRef or ByVal statement. Variables are declared with Type Characters, but subroutine parameters are missing a type declaration. I'm not sure about the Visual Basic version used. In the main file the first statement is VERSION 5.00
.
When I open the code in Visual Studio the type of the parameters are shown as Object
. So either Visual Studio knows something that I don't, or this is just because parameters are missing a type declaration.
I also own a compiled version of that software and the results it produces look fine. So when I'm finished with transcribing the code I can at least check the results against the original.
The default parameter passing is ByRef. You only have to specify ByVal parameters.
As for the type of the parameters being converted to Object in Visual Studio, this is because the default type in Visual Basic is "Variant", which for all intents and purposes maps to the System.Object type.