开发者

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

开发者 https://www.devze.com 2023-04-03 13:34 出处:网络
In vb.net the methods have their parameters using ByVal by default, it\'s better practice / common practice to make it explicit?

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 Strin开发者_C百科g)
{
   ' ...
}
End Sub

Without ByVal:

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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号