vb.netc#-to-vb.netout-parameters

Is there a VB.NET equivalent of C# out parameters?


Does VB.NET have a direct equivalent to C# out function parameters, where the variable passed into a function does not need to be initialised?


Solution

  • No, there is no equivalent of the out keyword in VB.

    However, VB does automatically initialise all local variables in a method, so you can use ByRef without needing to explicitly initialise the variable first.

    Example:

    Sub Main()
      Dim y As Integer
      Test(y)
    End Sub
    
    Sub Test(ByRef x As Integer)
      x = 42
    End Sub
    

    (If you examine code in the framework (for example Double.TryParse), you may see the <OutAttribute> added to parameters, but that only makes a difference when the call is marshalled for COM interop or platform invoke.)