Hi friends.in this article i will explain about ByVal Versus ByRef Example Or Difference between Byval and ByRef.
By Value and By
Reference are two ways of passing arguments to a method. If you pass a value by
value, means you send a copy of the value to a method and so changes made to
that copy wont affect the original variable in a caller method.
But when you pass them by reference, means you pass a reference to the same value in a memory, so any changes done in the called method, will affect the sender method variable passed . You use By Ref if you need the changes in the called method to affect the variable in the caller method. It is like returning the changes.
Partial Class _ Default
Inherits
System.Web.UI.Page
Protected
Sub Page_Load(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles
Me.Load
Dim y As Integer = 1
ex(y)
Response.Write(y &
"<br/>")
Dim y2 As Integer = 2
ex2(y2)
Response.Write(y2)
End
Sub
Protected
Sub ex(ByVal
x As Integer)
x = 10
End
Sub
Protected
Sub ex2(ByRef
x As Integer)
x = 10
End
Sub
End Class
|
The output of y after
calling a method will be 1 because changes in the called method does not affect
there.But the output y2 after calling a method will be 10 because
changes in the called method does effect there.
No comments:
Post a Comment