VB is rather looser with ByRef
parameters than C# is. For example, it allows you to pass properties by reference. C# doesn't allow this.
In a similar way, with Option Strict off, VB allows you to use an argument which is a subtype of the declared parameter. As a short but complete program, consider this:
Imports System
Public Class Test
Public Shared Sub Main(args As String())
Dim p As String = "Original"
Foo(p)
Console.WriteLine(p)
End Sub
Public Shared Sub Foo(ByRef p As Object)
p = "Changed"
End Sub
End Class
That works in VB, but the equivalent in C# wouldn't... and for good reason. It's dangerous. In this case, we're using a string variable and we happen to change p
to refer to another string, but if we change the body of Foo
to:
p = new Object()
Then we get an exception at execution time:
Unhandled Exception: System.InvalidCastException: Conversion from type 'Object' to type 'String' is not valid.
Basically ref
is compile-time type-safe in C#, but ByRef
isn't type-safe in VB with Option Strict off.
If you add:
Option Strict On
to the program in VB, however (or just change the defaults for your project) you should see the same problem in VB:
error BC32029: Option Strict On disallows narrowing from type 'Object' to type
'String' in copying the value of 'ByRef' parameter 'p' back to the matching
argument.
Foo(p)
~
This suggests that you're currently coding without Option Strict... I'd recommend using it ASAP.