Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 622
Yogurtu Avatar asked Feb 09 '12 08:02

Yogurtu


People also ask

What is the difference between ByVal and ByRef?

ByRef = You give your friend your term paper (the original) he marks it up and can return it to you. ByVal = You give him a copy of the term paper and he give you back his changes but you have to put them back in your original yourself.

Is VBA default ByVal or ByRef?

ByRef (Default)Passing variables by reference is the default and this means that you pass a pointer to the variable and therefore the variable's value can be changed inside the procedure or function.

Is ByRef faster than ByVal?

With a large Byte() array, ByVal is faster than ByRef , but it's also negligible.

Is ByVal or ByRef C#?

C# Passing arguments by default is ByRef instead of ByVal.


2 Answers

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 
like image 164
Tim Schmelter Avatar answered Sep 22 '22 22:09

Tim Schmelter


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.

like image 23
Jay Avatar answered Sep 21 '22 22:09

Jay