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?
相关问题
- 'System.Threading.ThreadAbortException' in
- how to use special characters like '<'
- C# to VB - How do I convert this anonymous method
- Scaling image for printing
- visual basics equal to or greater than
相关文章
- vb.net 关于xps文件操作问题
- Checking for DBNull throws a StrongTypingException
- Using the typical get set properties in C#… with p
- Load a .NET assembly from the application's re
- C# equivalent of VB DLL function declaration (Inte
- What other neat tricks does the SpecialNameAttribu
- Automatically install updates with ClickOnce deplo
- Resetting Experimental instance of Visual Studio
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.
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 aByRef
parameter definition, although VB ignores it, is treated by C# as anout
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
.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
to
It also helps when coding if variable names are descriptive...
Use keyword ByRef before variable.
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.
C# version
Vb.net version
Found the answer here
Update
As stated in the comment do not forget to initialze your parameter that will be used in the out slot