rfc1484
rfc1484

Reputation: 9847

Using ByVal in vb.net methods, what's the common practice?

In vb.net the methods have their parameters using ByVal by default, it's better practice / common practice to make it explicit?

For example:

With ByVal:

Private Sub MySub(ByVal Q As String)
{
   ' ...
}
End Sub

Without ByVal:

Private Sub MySub(Q As String)
{
   ' ...
}
End Sub

Upvotes: 5

Views: 1786

Answers (3)

kodkod
kodkod

Reputation: 1576

Starting with VS 2010 SP1, ByVal is no longer automatically inserted by the IDE.

I personally think it's better not to insert ByVal manually, because:

  1. it's the default passing mechanism anyway, if neither ByVal nor ByRef are explicitly specified.
  2. omitting ByVal from method signature makes ByRef stand out.
  3. it adds 'noise' to the code. VB.Net is already very verbose, no need to clutter the code with unnecessary ByVals.

Upvotes: 3

KV Prajapati
KV Prajapati

Reputation: 94643

It is common practice that a method arguments can be specified at ByValue or ByReference. In VB.NET, the default argument type is ByVal. In many programming language, method arguments are by-value by default. If argument is not qualified with ByVal or ByRef then the argument type will be ByVal.

Upvotes: 0

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239764

According to Microsoft:

It is good programming practice to include either the ByVal or ByRef keyword with every declared parameter.

And if you use Visual Studio, it defaults to inserting ByVal if you don't explicitly specify it.

Upvotes: 5

Related Questions