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

前端 未结 3 1863
粉色の甜心
粉色の甜心 2020-12-01 13:49

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

相关标签:
3条回答
  • 2020-12-01 14:18

    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
    
    0 讨论(0)
  • 2020-12-01 14:30

    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.

    0 讨论(0)
  • 2020-12-01 14:45

    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.

    0 讨论(0)
提交回复
热议问题