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

vb.netC# to-vb.netOut Parameters

vb.net Problem Overview


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?

vb.net Solutions


Solution 1 - vb.net

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.)

Solution 2 - vb.net

No, there is no equivalent construct that allows a non-initialised variable to be passed to a method without a warning, but, as mentioned in my question and answer specifying an <Out()> attribute on a ByRef parameter definition, although VB ignores it, is treated by C# as an out parameter.

So, I would pre-initialise reference variables to Nothing and specify <Out()> ByRef to signify the intention (that will work if C# users ever access your methods).

If you feel you know when you intend to access the default Nothing in otherwise unassigned reference variables you can set the "Warning configuration" "Use of variable prior to assignment" to "None" at the Project level (Project Properties > Compile, and you probably want to set Configuration to "All Configurations" before changing this setting), or, in VS2015 (VB.NET 14), you can use #Disable Warning BC42030.

Solution 3 - vb.net

C# version

  void TestFunc(int x, ref int y, out int z) {
  x++;  
  y++;
  z = 5;
}

Vb.net version

    Sub TestFunc(ByVal x As Integer, ByRef y As Integer, ByRef z As Integer)
  x += 1
  y += 1 
  z = 5 
End Sub

[Found the answer here][1]

Update

As stated in the comment do not forget to initialze your parameter that will be used in the out slot

[1]: http://www.harding.edu/fmccown/vbnet_csharp_comparison.html "Vb.net vs C# comparison"

Solution 4 - vb.net

I had the problem in VB.NET that I called a function "by ref" that passed an array back.

Even though the compiler flagged it as a warning it was fine. The fix is super simple and probably good programming practice.

I changed

Dim m_arr_values() as Integer

fnRetArray(m_arr_values)

to

' Even though 'Nothing' is the default value, setting it
' stops the compiler complaining.
Dim m_arr_values() as Integer = Nothing

fnRetArray(m_arr_values)

It also helps when coding if variable names are descriptive...

Sub fnCreatePalette(ByRef arr_in_pal() As color, ByRef arr_out_pal() as uinteger)
    ...
End Sub

Solution 5 - vb.net

VB has the attribute which should be the same as C# out but today you still get a warning even if you use it. There are details about fixing it in vblang area of github. https://github.com/dotnet/vblang/issues/67.

Solution 6 - vb.net

You can use the pass by reference method in VB.NET.

You need the Out parameter mechanism in C#, because it doesn't let you use any variable without initializing it.

VB.NET doesn't need a special keyword as it automatically does it by itself.

Just use ByRef.

Solution 7 - vb.net

Use keyword ByRef before variable.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncspoltonView Question on Stackoverflow
Solution 1 - vb.netGuffaView Answer on Stackoverflow
Solution 2 - vb.netMark HurdView Answer on Stackoverflow
Solution 3 - vb.netDavidView Answer on Stackoverflow
Solution 4 - vb.netTheWhitdeView Answer on Stackoverflow
Solution 5 - vb.netPaul CohenView Answer on Stackoverflow
Solution 6 - vb.netJoshView Answer on Stackoverflow
Solution 7 - vb.netdeostrollView Answer on Stackoverflow