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?

- 30,738
- 21
- 105
- 131

- 4,495
- 4
- 26
- 34
7 Answers
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.)

- 687,336
- 108
- 737
- 1,005
-
Is there any way to avoid the compilation warnings, without disabling the warnings. – cspolton Dec 05 '10 at 12:34
-
Yes I've read about the `
`, fortunately I'm migrating a classic ASP app to VB.NET, it's not COM interop or platform invoke. I have hundreds of compiler warnings to deal with. – cspolton Dec 05 '10 at 12:41 -
@Spolto: I tested this in both VS 2008 and VS 2010, with explicit and strict mode on, and I don't get any warnings. I added the example above. – Guffa Dec 05 '10 at 12:43
-
2@Spolto: If you are translating VBScript to VB, you should make sure to set Explicit and Strict mode on. It will get you more error messages, but most will point to the source of problems (e.g. variable declared without type) rather than secondary problems (e.g. variable declared without type becomes `Object`, so it can't be used for a `ByRef x As Integer` parameter). – Guffa Dec 05 '10 at 12:57
-
5@Guffa: I don't know if it's a version thing, but I too get compiler warnings when passing uninitialized *reference type* variables as `ByRef` parameters. (It doesn't happen with value type parameters.) – Dan Tao Dec 05 '10 at 14:12
-
I'll post a representative code example tomorrow, I don't have access to Visual Studio right now. – cspolton Dec 05 '10 at 14:32
-
4@Dan Tao, Spolto: That seems to be the difference, I get a warning with reference types also. The inability to specify out parameters is a limitation in the language, and you just have to initialise the variables to get rid of the warnings. Even assigning `Nothing` to them will get rid of the warning eventhough it doesn't change the result. – Guffa Dec 05 '10 at 15:59
-
2@Guffa: Yes, I've been assigning `Nothing` to them so far. It's just time consuming as I'm having to do it hundreds of times in a large legacy website. Thank you for investigating. – cspolton Dec 05 '10 at 17:30
-
1The downvote may have been because the `
` attribute is equivalent to the C# version, as my answer points out. – Mark Hurd Nov 29 '11 at 06:53 -
3@MarkHurd: Then the downvote is unjust, because I have already covered that the `Out` attribute is *not* equivalent to the C# `out` keyword. – Guffa Nov 29 '11 at 10:14
-
1@Guffa: In that case please add an answer to [my question](http://stackoverflow.com/questions/6744173/does-specifying-the-outattribute-on-byref-internal-methods-currently-do-anything/6760527#6760527) correcting my answer, showing the differences between a C# `out` parameter and a VB.NET `
ByRef` parameter. – Mark Hurd Nov 29 '11 at 18:52
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
.
-
3This is significant. I had a VB subclass of MembershipProvider and then a C# subclass of the VB subclass. The C# code was not recognizing the fact that the abstract methods in the MembershipProvider had already been implemented until I applied the attribute in the VB class for parameters that were specified as out in the MembershipProvider base class. – Richard Collette Mar 08 '14 at 19:33
-
@RichardCollette That's probably worth being an answer to my linked question! – Mark Hurd Mar 09 '14 at 10:50
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
Update
As stated in the comment do not forget to initialze your parameter that will be used in the out slot

- 4,185
- 2
- 27
- 46
-
In general, I agree that *ByRef* is the closest thing to the *out*. However, *ByRef* will still throw a warning if you pass a variable uninitialized, as the question asks. – Richard Jun 12 '12 at 21:00
-
My downvote was from quite a while ago: The web site you've linked to is very general; it does not list specific issues, differences and technicalities. Also your answer still does not answer the question. – Mark Hurd Jun 14 '12 at 03:25
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

- 30,738
- 21
- 105
- 131

- 21
- 2
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.

- 241
- 4
- 14
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.

- 30,738
- 21
- 105
- 131

- 29
- 1
-
3This does not answer the question, and it's wrong with respect to C#. – cspolton Sep 04 '12 at 10:14
-
The byref allows you to not initialize, and allows you to change the params value. But, as opposed to the C# out param, it DOES ALLOW you to initialize the parameter with a value and use it in the function, whereas in C# the out keyword forces you to use this ONLY as an out-parameter and NOT as input to the function. Also, if you do not change or set a value to this param within you function, the compiler will not catch that as an error, as opposed to C# where a compilation error will be issued. – pashute Dec 30 '16 at 05:54
-
4`ByRef` is equivalent to `ref` parameters in C#, which need to be initialised before being passing into a function. – cspolton Dec 05 '10 at 12:31