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.
|
| Author: Madhu 28 May 2004 | Member Level: Bronze Points : 0 |
I appreciate your idea, it was good but. If you re-initialize the value for an object then the latest value will be available.
If I =10 I = 20
What could the value of I now
Byval : You cannot change the value Byref : You can change the value
Your program is misleading because; these two key words will play key roll in changing the parameter value.
If this is Byval this will not happen byref this will happen
That is the reason Microsoft has changed parameter as ByVal in .NET it was Byref in VB 6.0
I feel that this program gives an impression that there is no difference in Byval and Byref
|
| Author: Madhu 28 May 2004 | Member Level: Bronze Points : 0 |
I appreciate your idea, it was good but. If you re-initialize the value for an object then the latest value will be available.
If I =10 I = 20
What could the value of I now
Byval : You cannot change the value Byref : You can change the value
Your program is misleading because; these two key words will play key roll in changing the parameter value.
If this is Byval this will not happen byref this will happen
That is the reason Microsoft has changed parameter as ByVal in .NET it was Byref in VB 6.0
I feel that this program gives an impression that there is no difference in Byval and Byref
|
| Author: Sadha Sivam 28 May 2004 | Member Level: Gold Points : 0 |
hi madhu,
U have mistaken, i have written what holds good for an object and not for a variable please try that for an object and let me know.
With regards,
Sadha Sivam S
|
| Author: niyazi sanjar 25 Sep 2004 | Member Level: Bronze Points : 0 |
Class Object are always passed byReference in .NET. So shouldnt really matter if it is ByVal or ByRef.
|
| Author: Paul Waldschmidt 16 Oct 2004 | Member Level: Bronze Points : 0 |
You are working with a class object. In .NET, classes are always passed ByRef, unlike value types.
|