vb.net 为什么不再需要指示 ByVal/ByRef?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9207620/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:56:13  来源:igfitidea点击:

Why is it not necessary to indicate ByVal/ByRef anymore?

vb.netvisual-studio-2010byrefbyval

提问by Yogurtu

I just installed Visual Studio 2010 Service pack (proposed on Windows Update), and I can see a new feature on the "intellisense" that means when I write a Functionor Subin VB.NET it doesn't auto-complete parameters with ByRefor ByVal...

我刚刚安装了 Visual Studio 2010 Service Pack(在 Windows 更新中提出),我可以在“智能感知”上看到一个新功能,这意味着当我在 VB.NET 中编写 aFunction或 时Sub,它不会自动完成参数ByRefByVal.. .

1) Is there anyway that I can configure this option back to how it was before?

1) 无论如何,我可以将此选项配置回以前的状态吗?

2) If I don't specify ByX, which one is used by default? (it seems like it is always ByRef)

2)如果我不指定ByX,默认使用哪个?(似乎总是如此ByRef

采纳答案by Jay

Tim covered what you asked directly, but something else to keep in mind is that any reference type variable, like a user defined class even if passed by value will allow you to make changes to that instances properties etc that stay. It won't however allow you to change the entire object. Which may be why it seemed to you to be defaulting to by reference

Tim 涵盖了您直接询问的内容,但要记住的另一件事是,任何引用类型变量,例如用户定义的类,即使按值传递也将允许您更改该实例属性等。但是,它不允许您更改整个对象。这可能就是为什么在您看来默认为引用

Public Sub (Something As WhateverClass) 
  Something = New WhateverClass 'will result in no changes when outside this method

  Something.Property1 = "Test"  'will result in an updated property when outside this method
End Sub

From MSDN:

MSDN

The value of a reference type is a pointer to the data elsewhere in memory. This means that when you pass a reference type by value, the procedure code has a pointer to the underlying element's data, even though it cannot access the underlying element itself. For example, if the element is an array variable, the procedure code does not have access to the variable itself, but it can access the array members.

引用类型的值是指向内存中其他位置的数据的指针。这意味着当您按值传递引用类型时,过程代码有一个指向底层元素数据的指针,即使它不能访问底层元素本身。例如,如果元素是数组变量,则过程代码不能访问变量本身,但可以访问数组成员。

回答by Tim Schmelter

It seems that this post covers your question:

这篇文章似乎涵盖了您的问题:

http://msmvps.com/blogs/carlosq/archive/2011/03/15/vs-2010-sp1-changing-quot-byval-quot-vb-net-code-editor-experience.aspx

http://msmvps.com/blogs/carlosq/archive/2011/03/15/vs-2010-sp1-changed-quot-byval-quot-vb-net-code-editor-experience.aspx

So no, there is no way to get the old behaviour. From now on ByValis the default (what it was before) and it won't get added automatically to the method parameters.

所以不,没有办法获得旧的行为。从现在开始ByVal是默认值(之前是什么),它不会自动添加到方法参数中。

In my opinion this is a good decision since it's making VB.NET a bit more consistent with C# and avoids unnecessary "noises"(it's already verbose enough).

在我看来,这是一个很好的决定,因为它使 VB.NET 与 C# 更加一致并避免了不必要的“噪音”(它已经足够冗长了)。

Old behaviour:

旧行为:

Private Sub test(ByVal test As String)
End Sub

New behaviour

新行为

Private Sub test(test As String)
End Sub

回答by sancho.s ReinstateMonicaCellio

Beware when transferring routines to VBA, where the default is ByRef(see, e.g., "The Default Method Of Passing Parameters" at the bottom of this page, by the great Chip Pearson). That can be messy.

将例程传输到 VBA 时要小心,默认值是ByRef(例如,请参阅本页底部的“传递参数的默认方法” ,由伟大的 Chip Pearson 撰写)。那可能会很乱。