为什么不再需要指明ByVal/ByRef?

发布于 2025-01-04 06:18:56 字数 310 浏览 2 评论 0 原文

我刚刚安装了 Visual Studio 2010 Service pack(在 Windows 更新上建议),我可以在“intellisense”上看到一个新功能,这意味着当我编写 FunctionSub 时在 VB.NET 中,它不会使用 ByRefByVal 自动完成参数...

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

2)如果我不指定ByX,默认使用哪一个? (看起来总是ByRef

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 Function or Sub in VB.NET it doesn't auto-complete parameters with ByRef or ByVal...

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

望喜 2025-01-11 06:18:56

看来这篇文章涵盖了您的问题:

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

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

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

旧行为:

Private Sub test(ByVal test As String)
End Sub

新行为

Private Sub test(test As String)
End Sub

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

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

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

Old behaviour:

Private Sub test(ByVal test As String)
End Sub

New behaviour

Private Sub test(test As String)
End Sub
眼波传意 2025-01-11 06:18:56

蒂姆涵盖了您直接询问的内容,但要记住的其他事情是任何引用类型变量(例如用户定义的类)即使按值传递也将允许您对保留的实例属性等进行更改。但是它不允许您更改整个对象。这可能就是为什么您似乎默认引用

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

来自 MSDN

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

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

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:

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.

月依秋水 2025-01-11 06:18:56

将例程传输到 VBA 时要小心,默认值为 ByRef(请参阅 本页底部,作者:伟大的 Chip Pearson)。
这可能会很混乱。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文