What is the main difference between ByVal and ByRef? Where are they used in Visual Basic Programming? Explain your answers with examples.
Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Sub Main()
Dim message As String = “Hello”
AppendExclamationByRef(message)
Console.WriteLine(message) ‘ Output: Hello!
End Sub
Sub AppendExclamationByRef(ByRef str As String)
str = str & “!”
End Sub
Sub Main()
Dim message As String = “Hello”
AppendExclamationByVal(message)
Console.WriteLine(message) ‘ Output: Hello
End Sub
Sub AppendExclamationByVal(ByVal str As String)
str = str & “!”
End Sub
Choosing between
ByVal
andByRef
depends on whether you want the called function to have the ability to modify the original variable or not.