Hi,
What is ByVal and ByRef means?
Hi,
What is ByVal and ByRef means?
The word ByVal is short for "By Value". What it means is that you are passing a copy of a variable to your Subroutine. You can make changes to the copy and the original will not be altered.
ByRef is the alternative. This is short for By Reference. This means that you are not handing over a copy of the original variable but pointing to the original variable. Let's see a coding example.
Add a new button the form you created in the previous section. Double click the button and add the following code:
You'll get a wiggly line under IncrementVariable(Number1). To get rid of it, add the following Subroutine to your code:Code:Dim Number1 As Integer Number1 = 10 Call IncrementVariable(Number1) MsgBox(Number1)
When you're done, run the program and click your new button. What answer was displayed in the message box?Code:Private Sub IncrementVariable(ByVal Number1 As Integer) Number1 = Number1 + 1 End Sub
It should have been 10. But hold on. Didn't we increment the variable Number1 with this line?
So Number1 started out having a value of 10. After our Sub got called, we added 1 to Number1. So we should have 11 in the message box, right?Code:Number1 = Number1 + 1
The reason Number1 didn't get incremented was because we specified ByVal in the Sub:
This means that only a copy of the original variable got passed over. When we incremented the variable, only the copy got 1 added to it. The original stayed the same - 10.Code:ByVal Number1 As Integer
Change the parameter to the this:
Run your programme again. Click the button and see what happens.Code:ByRef Number1 As Integer
This time, you should see 11 displayed in the message box. The variable has now been incremented!
It was incremented because we used ByRef. We're referencing the original variable. So when we add 1 to it, the original will change.
The default is ByVal - which means a copy of the original variable. If you need to refer to the original variable, use ByRef.
If you pass an argument by reference when calling a procedure, the procedure access to the actual variable in memory. As a result, the variable's value can be changed by the procedure.
If you pass an argument by value when calling a procedure, the variable's value can be changed with in the procedure only outside the actual value of the variable is retained.
Passing by reference is the default in VBA. If you do not explicitly specify to pass an argurment by value, VBA will pass it by reference.
The working of ByVal & ByRef in Vb.Net
As we all know we can pass the variable either ByVal or ByRef.
Ok, Let have close look at the following code snippet.
I have class TestClass as shown below
Public Class TestClass
Public str As String = "Hai i am the original...."
End Class
'In the form place a command button(lets call that as Button1) and paste the following code in the form.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tes As New TestClass()
PassByVal(tes)
MsgBox(tes.str)
End Sub
Private Sub PassByVal(ByVal testobj As TestClass)
testobj.str = "String is changed"
End Sub
Now what will be displayed in the message box.
We all know that when a parameter is passed byval a copy of it is sent and whatever changes we make will not affect the original.
So, Now Take some time and tell what will be the output.
****************************************************
If you have say "Hai i am the original...." will be displayed. You are wrong.
If you have chosen correctly, no problem.
Actually it shows "String is changed".
Explanation,
Because, even if you pass an object as a ByVal parameter, it is passed internally as ByRef.
Since we are passing an object of TestClass . Even though the function PassByVal takes it as the ByVal parameter, it will be passed as ByRef.
Bookmarks