Search code examples
c#.netvb.nettranslate

VB.NET Function with no return value, converted to C# gets return errors?


Converting some VB.NET code. Some of the static functions do some work on some parameters passed by reference, but does not return anything. What exactly is going on in the VB.NET functions that they can exist without having a return value and not get any debug errors? What happens to the boolean?

Overloads Shared Function ExampleMethod(ByRef buffer1() as Byte, ByRef buffer2() as Byte) As Boolean
'do stuff here, no return types
End Function

Overloads Shared Function ExampleMethod(ByRef buffer1() as Byte, ByRef buffer2 as Byte) As Boolean
'do stuff here, no return types
End Function

Solution

  • See https://msdn.microsoft.com/en-us/library/sect4ck6.aspx

    With VB.Net you can return a value by either using the Return statement, or assigning a value to the Function Name, e.g.:

        ExampleMethod = true
        Exit Function
    End Function
    

    It goes on to say:

    If you use Exit Function without assigning a value to name, the procedure returns the default value for the data type that's specified in returntype. If returntype isn't specified, the procedure returns Nothing, which is the default value for Object.

    C# is a bit more strict!