34

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)

2
  • 2
    The default is ByVal, but be sure you understand what passing by value and by reference actually means. Commented Feb 9, 2012 at 8:50
  • 11
    You almost always want to pass ByVal, and this is in fact the default. There's no reason for the code to be cluttered up with ByVal all over the place; that's just "noise". Only specify ByRef explicitly when you absolutely must have pass-by-reference semantics. Commented Feb 9, 2012 at 8:51

3 Answers 3

44

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
20

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.

1
  • 2
    This answer is kinda of misleading since it explains "how the value of a reference type works" whereas the question is "why is it not necessary to indicate ByVal/ByRef anymore?"
    – mchar
    Commented Jun 5, 2017 at 11:20
16

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.

1
  • The same is true when transferring routines to VB6 where the default is also ByRef
    – MarkJ
    Commented Aug 1, 2019 at 11:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.