dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.23k stars 5.88k forks source link

ByRef is the default, not ByVal, when using Sub statements with parameters #22566

Closed ghost closed 3 years ago

ghost commented 3 years ago

[Enter feedback here]

Hi Microsoft Docs team, I discovered an inaccuracy in the documentation article about ByVal, found at: https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/byval.

The documentation states that "If no modifier is specified, ByVal is the default. ... Because it is the default, you do not have to explicitly specify the ByVal keyword in method signatures. It tends to produce noisy code and often leads to the non-default ByRef keyword being overlooked."

However, as my Excel VBA code below demonstrates, ByRefis the default value, not ByVal, when using parameters with Substatements.

Sub firstSub()
    Dim varOne As Byte
    varOne = 1
    Debug.Print "Value of varOne: ", varOne
    Call secondSub(varOne)
    Debug.Print "Value of varOne is now: ", varOne
End Sub

Sub secondSub(varTwo As Byte)
    Debug.Print "Value of varTwo is: ", varTwo
    varTwo = 2
    Debug.Print "Value of varTwo is now: ", varTwo
End Sub

' Prints the following:
' Value of varOne:             1 
' Value of varTwo is:          1 
' Value of varTwo is now:      2 
' Value of varOne is now:      2 

' Now, showing difference when ByVal keyword is used:
Sub firstSub()
    Dim varOne As Byte
    varOne = 1
    Debug.Print "Value of varOne: ", varOne
    Call secondSub(varOne)
    Debug.Print "Value of varOne is now: ", varOne
End Sub

Sub secondSub(ByVal varTwo As Byte)
    Debug.Print "Value of varTwo is: ", varTwo
    varTwo = 2
    Debug.Print "Value of varTwo is now: ", varTwo
End Sub

' Prints the following:
' Value of varOne:             1 
' Value of varTwo is:          1 
' Value of varTwo is now:      2 
' Value of varOne is now:      1

Also, here's another source that confirms that ByRef is the default: https://bettersolutions.com/vba/macros/byval-or-byref.htm


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Youssef1313 commented 3 years ago

I don't know about VBA. However, this documentation is for VB.NET, not VBA. The information is correct for VB.NET.

ghost commented 3 years ago

Thank you, Youssef. I appreciate your response, and that makes sense that it could be different in .NET vs VBA. I found this page when searching “Excel VBA is ByRef or ByVal defaulthttps://www.google.com/search?q=excel+vba+is+byval+or+byref+default” and also “ByVal VBAhttps://www.google.com/search?q=byval+vba,” so I assumed that the reference pertained to VBA without even checking. Thank you for clarifying that for me.

Ryan Parker

From: Youssef Victormailto:notifications@github.com Sent: Saturday, January 30, 2021 10:47 AM To: dotnet/docsmailto:docs@noreply.github.com Cc: Ryan Parkermailto:Ryan.Parker2@Outlook.com; Authormailto:author@noreply.github.com Subject: Re: [dotnet/docs] ByRef is the default, not ByVal, when using Sub statements with parameters (#22566)

I don't know about VBA. However, this documentation is for VB.NET, not VBA. The information is correct for VB.NET.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/dotnet/docs/issues/22566#issuecomment-770250268, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AQTOKID66KKNYQGHR6VTXALS4RAZTANCNFSM4WZL2WZA.

BillWagner commented 3 years ago

closing per conversation.