是否有等效于 C# 输出参数的 VB.NET?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4358742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a VB.NET equivalent of C# out parameters?
提问by cspolton
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 是否直接等效于 C#out
函数参数,其中传递给函数的变量不需要初始化?
回答by Guffa
No, there is no equivalent of the out
keyword in VB.
不,out
在 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.
但是,VB 会自动初始化方法中的所有局部变量,因此您可以使用ByRef
而无需先显式初始化变量。
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.)
(如果您检查框架中的代码(例如Double.TryParse),您可能会看到<OutAttribute>
添加到参数,但只有在为 COM 互操作或平台调用编组调用时才会有所不同。)
回答by Mark Hurd
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 answerspecifying an <Out()>
attributeon a ByRef
parameter definition, although VB ignores it, istreated by C# as an out
parameter.
没有,没有相当的结构,它允许非初始化变量传递到方法,无需警告,但是,正如我所提到的问题和答案指定<Out()>
属性上的ByRef
参数定义,虽然VB忽略它,是用C#处理作为out
参数。
So, I would pre-initialise reference variables to Nothing
andspecify <Out()> ByRef
to signify the intention (that will work if C# users ever access your methods).
所以,我会预先初始化引用变量Nothing
并指定<Out()> ByRef
来表示意图(如果 C# 用户曾经访问过你的方法,这将起作用)。
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
.
如果你觉得你知道,当你打算访问默认Nothing
另有未分配的参考变量,你可以设置“警告配置” “的使用变量之前分配”到“无”在项目一级(项目属性>编译,你可能要在更改此设置之前将配置设置为“所有配置”),或者,在 VS2015(VB.NET 14)中,您可以使用#Disable Warning BC42030
.
回答by David
C# version
C#版本
void TestFunc(int x, ref int y, out int z) {
x++;
y++;
z = 5;
}
Vb.net version
vb.net 版本
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
如评论中所述,不要忘记初始化将在 out 插槽中使用的参数
回答by TheWhitde
I had the problem in VB.NET that I called a function "by ref" that passed an array back.
我在 VB.NET 中遇到了问题,我调用了一个“by ref”函数,该函数将数组传回。
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
回答by Paul Cohen
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.
VB 具有与 C# out 相同的属性,但今天即使您使用它,您仍然会收到警告。在 github 的 vblang 区域中有关于修复它的详细信息。https://github.com/dotnet/vblang/issues/67。
回答by Josh
You can use the pass by reference method in VB.NET.
您可以在 VB.NET 中使用通过引用传递方法。
You need the Out parameter mechanism in C#, because it doesn't let you use any variable without initializing it.
您需要 C# 中的 Out 参数机制,因为它不允许您在未初始化的情况下使用任何变量。
VB.NET doesn't need a special keyword as it automatically does it by itself.
VB.NET 不需要特殊的关键字,因为它自己会自动完成。
Just use ByRef.
只需使用 ByRef。